一对多的休眠关系以不同方式保存主键。帮助我

时间:2015-10-29 07:29:01

标签: java hibernate postgresql spring-mvc

我有两个实体预订('One side')和GuestDeatails('Many side')DTO。我在这两个实体中有OneToMany关系 当我正在保存预订('单面')时,主键是在“预订”表中为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();
    }

OutPut for Single Record如:

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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

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

2 个答案:

答案 0 :(得分:1)

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

@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的序列。

答案 1 :(得分:0)

不要依赖自动生成的ID是连续的!

可能存在差距的原因很多 -

  • 在您的情况下,您正在为两个表使用一个生成器(序列)
  • 可能有两个线程同时生成ID,如果第一个然后调用rolback会怎么样?
  • 出于性能原因,如果数据库服务器崩溃,可能会有一些缓存丢失,产生一个&#34;漏洞&#34;顺序

数据库自动生成的 ID仅保证唯一性 - 不会生成相同的两个ID。它确实不保证ID将是顺序的,或者将使用序列中的所有数字。

如果您绝对要求ID是顺序的,那至少要求您在生成前一个ID的事务结束之前不生成另一个ID - 这是一个巨大的性能瓶颈。