如何在Hibernate中使用mappedBy更新集合类型关系?
我有两个相关的实体,比如说,
@Entity
public class Book {
@ManyToOne
Shelf shelf;
}
@Entity
public class Shelf {
@OneToMany(mappedBy="shelf")
Set<Book> books;
}
如果我获取一个空书架(没有书籍),创建一本新书并将其保留到书架上,然后再次获取该书架,则其书籍集合为空。 当我使用调试日志记录运行它时,我看到 Hibernate 不会第二次搜索书架,它只是从会话缓存中返回它,而它所在的位置并不知道它的书籍收藏已被更新。
如何消除影响并获取货架的更新状态?
谢谢,
阿尔乔姆。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎您必须在单个会话(事务)的范围内手动维护它。 @Cascade 和 EAGER 都不影响会话缓存
Seems like you have to maintain it by hand in the scope of a single session (transaction). Neither @Cascade nor EAGER influence session cache
尝试为 Shelf 中设置的书籍设置获取类型 EAGER:
Try to set fetch type EAGER for books set in Shelf:
@Cascade 是您要找的吗?
Is @Cascade what you are looking for?