Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of Hibernate Lazy loading

Shulou Source: shulou.com Published: 2022-06-02 07:59:38 09月28日 Update

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

Tags: Problems transactions code objects manual time files learning testing so that content control configuration great cost that is ideas performance situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Xiaomi OPPO Reno macOS