我有两个实体Reservation('One side')和GuestDeatails('Many side')DTO。我在这两个实体中有一个一对多关系。
当我保存保留(“一侧”)时,主键的生成方式类似于保留表中的1,GuestDetails表中的2。如何解决这个问题?
我的实体是:

预订舱位:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "reservation_id")
private Integer reservation_id;

@OneToMany(mappedBy ="reservation",fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List<GuestDetails> guestDetails;


GuestDetails类:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "guest_details_id")
private Integer guestDetailsId;

@ManyToOne
@JoinColumn(name="reservation_id",insertable=true,updatable=true,nullable=false)
private Reservation reservation;


我的控制器正在通过POST请求进行调用。一些包含GuestDetails对象的预留对象列表将会出现:

MyController类别:

@RequestMapping(value="/saveResList", method=RequestMethod.POST,consumes="application/json")
public void saveList(@RequestBody ArrayList<Reservation> reservationList) {
    iReservationService.saveReservationList(reservationList);
}


我的DAO课程:

@Override
public void saveReservationList(List<Reservation> reservationList) {

    for(Reservation reservation : reservationList) {
        GuestDetails details = reservation.getGuestDetails().get(0);
        details.setReservation(reservation);

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.save(reservation);

        session.getTransaction().commit();
    }


单记录的输出类似:

Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')


Hibernate: insert into RESERVATION (agent_name, arrivaldate, arrivaltime,   billing_instruction_description, booker_name, booking_date, booking_type, business_source_description, cancel_date, confirm_date, contact_email, contact_person, contact_phone, corporate_name, departdate, departtime, double_room, group_name, guest_country, guest_firstname, guest_id, guest_lastname, guest_middlename, members, num_adult, num_child, num_rooms, package_name, plan_description, property_name, rate_code_id, remarks, room_no, room_type_description, sales_executive_name, salutation_description, segment_description, single_room, special_instructions, staynights, triple_room, updated_by, updated_date, updated_ip, reservation_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into GUEST_DETAILS (arrivedFrom, designation, dob, drivingLicenceNo, gender, guestAddress, guestCity, guestCountry, guestEmail, guestMobile, guestPincode, guestState, guestTelePhone, guestType, nationality, panNo, passportNo, proceedingTo, reservation_id, salutation_id, title, updatedBy, updatedDate, updatedIp, valid, visitType, guest_details_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


我的问题是主键正在保存,如:
在保留表中:1,3,5,7 ......
在GuestDetails中:2,4,6,8 ....
请帮帮我..

最佳答案

发生这种情况是因为您使用相同的序列来生成主键值。尝试这样做:

@SequenceGenerator(name = "reservationSEQ", sequenceName = "reservation_id_seq", allocationSize = 1)
@Id
@Column(name = "reservation_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "reservationSEQ")
private Integer id;


并以相同的方式:

@SequenceGenerator(name = "guestDetailsSEQ", sequenceName = "guest_details_id_seq", allocationSize = 1)
@Id
@Column(name = "guest_details_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cguestDetailsSEQ")
private Integer id;


当然,您必须在数据库上创建名为guest_details_id_seqreservation_id_seq的序列。

09-25 22:01