Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

Is @Cascade what you are looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top