简体   繁体   English

OOP风格的Hibernate延迟加载

[英]Hibernate Lazyloading in OOP-style

So, lets imagine that I have two objects: Course and Exercise . 因此,假设我有两个对象: CourseExercise Course Contains list of Exercises. 课程包含练习列表。

Lazy loading is enabled, and via Repository I get a Course that I'm interested in: 启用了延迟加载,并且通过存储库获得了我感兴趣的课程:

couserRepo.query(new ByNameSpec("courseName")). 

Now I want to acces Course exercises, but when I call course.getExerccises() I get a msg, that session is closed already. 现在,我想参加课程练习,但是当我调用course.getExerccises()我得到一个消息,该会话已经关闭。 So, I have to make another query to repo, that is absolutely not convinient. 因此,我必须进行另一个查询以回购,这绝对不方便。

What to do with this? 怎么办呢?

Remember that opening session per operation is antipattern. 请记住,每个操作的打开会话都是反模式。 See http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#session-per-operation for more information. 有关更多信息,请参见http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#session-per-operation

The scope of a Hibernate org.hibernate.Session is flexible but you should never design your application to use a new Hibernate org.hibernate.Session for every database operation. Hibernate org.hibernate.Session的范围是灵活的,但您绝对不应将应用程序设计为对每个数据库操作都使用新的Hibernate org.hibernate.Session。

I think you should consider Session-per-request pattern. 我认为您应该考虑每个请求会话模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM