简体   繁体   中英

JPA Many To One persistence issue at fetching data

I have two table as hotel and hotal_room_type . And there is Many To One mapping from hotal_room_type to hotel . Means Primary key of hotel is foreign key in hotal_room_type .

Now I am fetching hotal_room_type data usfing following code

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. For other HotelRoomTypes It gives Hotel object but having 0 in hotelId variable , means I am unable to receive Hotel object again.

I need to get complete Hotel entity for all 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. Like :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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