How can I update a collection type relationship with mappedBy in Hibernate?

I have two related objects, say

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

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

      

If I take an empty shelf (no books), create and save a new book on the shelf and then take that shelf again, its book collection is empty. When I run it with debug log I see that Hibernate is not looking for the shelf a second time, it just returns it from the session cache where it does not know that the book collection has been updated.

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

Thank you,
Artem.

+1


a source to share


3 answers


It looks like you have to keep it manually within the same session (transaction). Neither @Cascade nor EAGER affect the session cache



+2


a source


Try to set the EACHER type for the books specified in the shelf:



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

      

+1


a source


Is @Cascade what you are looking for?

+1


a source







All Articles