Reputation: 1
I have a issue regarding one to many mapping in hibernate 4.x and I am using mysql5.6.
First Let show you my 2 entities, first user entity,
package com.project.entities;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="UserBookingEntryTable")
public class UserBookingEntryClass {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int uid;
private String ubmobile_no;
private String ubname;
@OneToMany(cascade = {CascadeType.ALL},mappedBy="user")
private Collection<BookingServicesClass> bookings=new ArrayList<BookingServicesClass>();
public Collection<BookingServicesClass> getBookings() {
return bookings;
}
public void setBookings(Collection<BookingServicesClass> bookings) {
this.bookings = bookings;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUbmobile_no() {
return ubmobile_no;
}
public void setUbmobile_no(String ubmobile_no) {
this.ubmobile_no = ubmobile_no;
}
public String getUbname() {
return ubname;
}
public void setUbname(String ubname) {
this.ubname = ubname;
}
}
Second Entity -Booking Class
package com.project.entities;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "BookingServiceTable")
public class BookingServicesClass {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int booking_id;
private String first_name;
private String last_name;
private String mobile;
private String location;
private String booking_address;
private String booking_type;
private String landmark;
private int booking_pincode;
private Date booking_date;
@ManyToOne
@JoinColumn(name="usid")
private UserBookingEntryClass user;
public Integer getBooking_id() {
return booking_id;
}
public void setBooking_id(Integer booking_id) {
this.booking_id = booking_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getBooking_address() {
return booking_address;
}
public void setBooking_address(String booking_address) {
this.booking_address = booking_address;
}
public String getLandmark() {
return landmark;
}
public void setLandmark(String landmark) {
this.landmark = landmark;
}
public Integer getBooking_pincode() {
return booking_pincode;
}
public void setBooking_pincode(Integer booking_pincode) {
this.booking_pincode = booking_pincode;
}
public Date getBooking_date() {
return booking_date;
}
public void setBooking_date(Date booking_date) {
this.booking_date = booking_date;
}
public String getBooking_type() {
return booking_type;
}
public void setBooking_type(String booking_type) {
this.booking_type = booking_type;
}
public UserBookingEntryClass getUser() {
return user;
}
public void setUser(UserBookingEntryClass user) {
this.user = user;
}
}
// to add booking from a user:
public String addService(String fname, String lname, String mob,
String ser, String loc, String add, String lm, int pc, String bd) {
String res = "failure";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date convertedCurrentDate = null;
try {
convertedCurrentDate = sdf.parse(bd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BookingServicesClass bs = new BookingServicesClass();
UserBookingEntryClass ubs = new UserBookingEntryClass();
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
System.out.println("dddd");
try {
**//userid is auto generated**
ubs.setUbmobile_no(mob);
ubs.setUbname((fname + " " + lname));
// bs.setBid(1);
bs.setFirst_name(fname);
bs.setLast_name(lname);
bs.setMobile(mob);
bs.setLocation(loc);
bs.setBooking_type(ser);
bs.setBooking_address(add);
bs.setLandmark(lm);
bs.setBooking_pincode(pc);
bs.setBooking_date(convertedCurrentDate);
bs.setUser(ubs);
session.save(ubs);
session.save(bs);
// Commit the transaction
session.getTransaction().commit();
/*session.close();*/
res = "success";
System.out.println("ajax");
} catch (Exception e) {
System.out.println(e);
session.getTransaction().rollback();
res = "failure";
// success=false;
}
return res;
}
Now the issue is that one user can do more than one booking. So the code create a foreign key in booking table which is users key who is booking. but when the one user books more than one booking the Unique constraint Error is given. So i made the userid auto genrate which is not according to my needs, becuase if same user books two booking then userid should be same but when i do this it gives me unique constraint error.
Please tell how should I implement it.
Thanks
Upvotes: 0
Views: 52
Reputation: 684
First thing - id of User should not be auto generated, maybe the mobilenumber which will definitely be unique should be the identifier
Second thing - When you add the booking you will check if the user exists , if it exists add the booking to the user ,otherwise create the user and add the bookings, and then commit the user.
Try these and let me know.
BookingServicesClass booking = new BookingServicesClass ()
if(ubmobile_no!=null) {
UserBookingEntryClass user= session.get(UserBookingEntryClass .class, ubmobile_no);
if (user!= null) {
booking.setUser(user);
user.getBookings().add(booking);
} else {
//do nothing
}
}
Upvotes: 1