hibernate 延迟加载问题

If we want the publisher object can be accessed outside the session, there will be two possible
solutions. One is to initialize the publisher explicitly, we can call the method Hibernate.initialize()
for this task. This will force the publisher object to be loaded from database.
Session session = factory.openSession();
try {
Book book = (Book) session.get(Book.class, id);
Hibernate.initialize(book.getPublisher());
return book;
} finally {
session.close();
}
Another solution is to turn off the lazy initialization feature for this association. This may decrease
the performance as the publisher object will be loaded together with the book object every time.
<hibernate-mapping package="mo.org.cpttm.bookshop">
<class name="Book" table="BOOK">
...
<many-to-one name="publisher" class="Publisher" column="PUBLISHER_ID"
lazy="false" />
</class>
</hibernate-mapping>

posted on 2007-07-03 17:23 付轩 阅读(305) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(2)

随笔档案

相册

搜索

最新评论

阅读排行榜

评论排行榜