我对hibernate 4.x中的一对多映射存在问题,我使用的是mysql5.6。
首先让我们看看我的2个实体, 第一个用户实体,
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;
}
}
第二实体-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;
}
现在问题是一个用户可以进行多次预订。 因此代码在预订表中创建外键,该预订表是预订的用户密钥。 但是当一个用户预订多个预订时,会给出唯一约束错误。 所以我制作了不符合我需要的用户自动生成器, 因为如果同一个用户预订两个预订,那么userid应该是相同的,但是当我这样做时,它给了我唯一的约束错误。
请告诉我该如何实施。
由于
答案 0 :(得分:1)
第一件事 - 用户的ID不应该是自动生成的,也许肯定是唯一的移动号码应该是标识符
第二件事 - 当您添加预订时,您将检查用户是否存在,是否存在将预订添加到用户,否则创建用户并添加预订,然后提交用户。
试试这些并告诉我。
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
}
}