为什么需要Hibernate @onetomany关系?
我有两个实体图书馆和书籍,这些图书馆和书籍在春季启动项目中与Hibernate @onetomany相关联。通过getter函数在特定库中获取书籍会导致懒惰化的表现。我能找到的解决方案是在书籍实体中进行查询,并获取与图书馆ID相对应的所有书籍。因此,我在想,如果我们只能在书籍表中存储与图书馆相对应的密钥,为什么需要OneTomany关系。
I have two entities Library and Books which are associated by Hibernate @OneToMany in a spring boot project. Fetching books in a particular library through the getter functions renders a LazyInitialisationException. The solution that I could find was making a query in the Books entity and fetching all the books corresponding to the library-id of the library. So, I was thinking why is oneToMany relationship required if we can just store a key corresponding to library in the Books table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地存储密钥并不能提供任何一致性保证。此外,使用定义的OneTomany或许多人,您还可以定义级联类型(您只需要保存父实体,然后在单个事务中自动保存所有孩子)。
解决问题的快速方法是使用FetchType急切的方法,但我建议您修复任何错误配置的内容。
Simply storing a key doesn't provide any consistency assurances. Also, using defined OneToMany or ManyToOne you can also define the cascade types (you would only need to save the parent entity and then all the children would automatically be saved, in a single transaction).
The quick way to fix your problem would be to use FetchType EAGER, but I would recommend fixing whatever you have misconfigured.