views:

240

answers:

3

I have two related entities, say

@Entity
public class Book {
    @ManyToOne
    Shelf shelf;
}

@Entity
public class Shelf {
    @OneToMany(mappedBy="shelf")
    Set<Book> books;
}

If I fetch an empty shelf (no books), create and persist a new book to the shelf and then fetch that shelf again, its books collection is empty. When I run it with debug logging I see that Hibernate doesn't search for the shelf for the second time, it just returns it from the session cache, where it lies not knowing, that it's books collection has been updated.

How can I get rid of the effect and get the updated state of the shelf?

Thanks,
Artem.

+1  A: 

Try to set fetch type EAGER for books set in Shelf:

@Entity
public class Shelf {
    @OneToMany(mappedBy="shelf",fetch=FetchType.EAGER)
    Set<Book> books;
}
Daniel Murygin
I don't want it to be eager. That would hit performance badly
artemb
A: 

Is @Cascade what you are looking for?

qbn
It sounds like it is, but I can't get it working. Which CascadeType should I use?
artemb
A: 

Seems like you have to maintain it by hand in the scope of a single session (transaction). Neither @Cascade nor EAGER influence session cache

artemb