HibernateLazy加載問題怎么解決-創(chuàng)新互聯(lián)

這篇文章主要講解了“Hibernate Lazy加載問題怎么解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Hibernate Lazy加載問題怎么解決”吧!

站在用戶的角度思考問題,與客戶深入溝通,找到開封網(wǎng)站設(shè)計(jì)與開封網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋開封地區(qū)。

Hbm文件

<bean id="BookItemRepositoryImpl"
class="domain.repository.hibernate.BookItemRepositoryImpl">

<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">

classpath:domain/BookItem.hbm.xml
classpath:domain/BookStore.hbm.xml

true
true
false
false

org.hibernate.dialect.Oracle9Dialect


<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">

oracle.jdbc.driver.OracleDriver


jdbc:oracle:thin:@192.168.10.28:1521:VIPDBZJ

scott

tiger

Sping配置文件

最開始的時(shí)候,服務(wù)代碼是這樣寫的

public BookItem findById(long bookItemId) {
BookItem bookItem = (BookItem) getHibernateTemplate().load(
BookItem.class, new Long(bookItemId));
return bookItem;
}

JUNIT測試代碼如下

public class BookItemRepositoryTest extends TestCase {
BookItemRepository bookItemRepo=null;

protected void setUp() throws Exception {
ApplicationContext context=new
ClassPathXmlApplicationContext("spring-context.xml");
bookItemRepo=(BookItemRepository)context.getBean("BookItemRepositoryImpl");

}

public void testFindById(){
BookItem item=bookItemRepo.findById(30);
assertEquals("123456", item.getBook().getISBN());
assertEquals("QiuHongBookStore", item.getBookStore().getName());

}

}

測試運(yùn)行以后報(bào)下列錯(cuò)誤

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at domain.BookItem$$EnhancerByCGLIB$$2f924ddd.getBook()
at test.domain.repository.hibernate.BookItemRepositoryTest.testFindById(BookItemRepositoryTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

開始我在HBM文件中,把

<class name="BookItem" table="T_BookItem" lazy="true".....

然后就正常了。但是這樣的代價(jià)是取消延遲加載,在load的時(shí)候直接進(jìn)行加載。如果這樣的話,在對象很大的時(shí)候性能會(huì)很差。于是在網(wǎng)上查資料。這個(gè)主要是HibernateTemplate在加載的時(shí)候,采用是lazy 加載方法,因?yàn)镠ibernateTemplat 在load完成后自動(dòng)的關(guān)閉了Session,所以造成了LazyInitializationException異常。

我最開始將代碼改成如下,也能解決這個(gè)問題。

public BookItem findById(long bookItemId) {
BookItem bookItem = (BookItem)getSession().load(
BookItem.class, new Long(bookItemId));
return bookItem;
}

但是這樣改我心里面沒有地,我擔(dān)心會(huì)不會(huì)存在Session沒有關(guān)閉或者泄漏的問題。

后來我聯(lián)想如果在Spring中增加事務(wù)控制,那么HibernateTemplate是不是就不會(huì)在調(diào)用完成后馬上Close Session了呢?(如果Close了Session,那事務(wù)是不是都需要Commit或RollBack了呢?,那這樣的話還談什么事務(wù)控制呢)。于是我進(jìn)行了如下事務(wù)配置

<bean id="BookItemRepositoryImpl"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<bean
class="domain.repository.hibernate.BookItemRepositoryImpl">

PROPAGATION_REQUIRED

然后代碼也進(jìn)行了修改,執(zhí)行手工加載操作。

public BookItem findById(long bookItemId) {
BookItem bookItem = (BookItem) getHibernateTemplate().load(
BookItem.class, new Long(bookItemId));

getHibernateTemplate().initialize(bookItem); // 手工加載每個(gè)對象
getHibernateTemplate().initialize(bookItem.getBook()); // 手工加載每個(gè)對象
getHibernateTemplate().initialize(bookItem.getBookStore()); //手工加載每個(gè)對象
return bookItem;
}

再進(jìn)行測試就OK了!

感謝各位的閱讀,以上就是“Hibernate Lazy加載問題怎么解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Hibernate Lazy加載問題怎么解決這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

網(wǎng)站欄目:HibernateLazy加載問題怎么解決-創(chuàng)新互聯(lián)
文章URL:http://muchs.cn/article26/dphicg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、軟件開發(fā)靜態(tài)網(wǎng)站、定制開發(fā)、ChatGPT、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)