Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Hibernate Lazy loading

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the Hibernate Lazy loading problem", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to solve the Hibernate Lazy loading problem"!

Hbm file

Classpath:domain/BookItem.hbm.xml

Classpath:domain/BookStore.hbm.xml

True

True

False

False

Org.hibernate.dialect.Oracle9Dialect

Oracle.jdbc.driver.OracleDriver

Jdbc:oracle:thin:@192.168.10.28:1521:VIPDBZJ

Scott

Tiger

Sping profile

In the beginning, the service code was written like this

Public BookItem findById (long bookItemId) {

BookItem bookItem = (BookItem) getHibernateTemplate (). Load

BookItem.class, new Long (bookItemId))

Return bookItem

}

The JUNIT test code is as follows

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 ())

}

}

The following errors are reported after the test run

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 (NativeMethod)

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)

At first, in the HBM file, I put

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report