Hibernate二级缓存和关联
我的电影类与导演有@OneToOne关系,与演员有@ManyToMany关系。所有这些类(电影、导演和演员)类都以只读模式缓存,集合也是如此。
我使用Hibernate的二级缓存和ehCache。
这些关系是惰性的,所以我有以下问题:如果电影在没有调用 getDirector() 或 getActors() 的情况下进入二级缓存,如果随后在没有 Session.merge() 的情况下调用这些方法,我们将得到一个 LazyInitializationException ,这对我来说不是一个选择。
我想如果我可以挂钩“放入缓存”事件并调用我的惰性方法,我的问题就会得到解决。有这样的事吗?我不愿意编写自定义基础设施代码,因为二级缓存的打开/关闭取决于应用程序等。
我是否遗漏了其他内容?
谢谢。
My class Movie has @OneToOne relationship with Director and a @ManyToMany relationship with Actor. All these classes (Movie, Director and Actor) classes are cached in read-only mode, and so are the collections.
I use Hibernate’s second level cache with ehCache.
The relationships are LAZY, so I have the following problem: if a Movie makes it to the second level cache without calling getDirector() or getActors(), we will get a LazyInitializationException if these methods are called subsequently without Session.merge(), and this is not an option for me.
I guess that my problem would be solved if I could hook to the event “put in cache” and call my lazy methods. Is there such a thing? I am reluctant writing custom infrastructure code, because second level caching is turned on/off depending on the application, etc.
Am I missing something else?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果关系被缓存,那么延迟获取它们会带来什么好处?只需将它们转为 EAGER,您的问题就会得到解决。
If the relationships are cached, what do you gain by having them LAZY fetched? Simply turn them to EAGER and your problem will be solved.