简体   繁体   English

JPA提取数据时的多对一持久性问题

[英]JPA Many To One persistence issue at fetching data

I have two table as hotel and hotal_room_type . 我有两个表,分别是hotelhotal_room_type And there is Many To One mapping from hotal_room_type to hotel . hotal_room_typehotel映射为hotal_room_type Means Primary key of hotel is foreign key in hotal_room_type . 意味着hotel主键是hotal_room_type外键。

Now I am fetching hotal_room_type data usfing following code 现在,我使用以下代码获取hotal_room_type数据

Query query = entityManager.createQuery("from "
            + HotelsRoomType.class.getName() + " where event.id = "
            + eventId);
List<HotelsRoomType> list = query.getResultList();

When I get two records of HotelRoomType having same Hotel then I got Hotel only for first HotelRoomType. 当我获得两个具有相同酒店的HotelRoomType记录时,我仅获得第一个HotelRoomType的酒店。 For other HotelRoomTypes It gives Hotel object but having 0 in hotelId variable , means I am unable to receive Hotel object again. 对于其他HotelRoomTypes它给出了Hotel对象,但是hotelId变量中有0,这意味着我无法再次接收到Hotel对象。

I need to get complete Hotel entity for all HotelRoomTypes. 我需要获得所有HotelRoomTypes的完整酒店实体。 How to make this possible? 如何做到这一点?

Finally I found my answer. 终于我找到了答案。 I need to add cascade=CascadeType.ALL in the mapping of the Hotel entity. 我需要在Hotel实体的映射中添加cascade=CascadeType.ALL Like : 喜欢 :

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "hot_room_frn_hot_id", nullable = false)
public Hotel getHotel() {
    return this.hotel;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM