@Entity Set Entity lazy = False

Source: Internet
Author: User

Problem description

The following exception information is queried in the bean information times through Hibernate:

Org.hibernate.LazyInitializationException:could not initialize Proxy-no Session
At Org.hibernate.proxy.AbstractLazyInitializer.initialize (abstractlazyinitializer.java:132)
At Org.hibernate.proxy.AbstractLazyInitializer.getImplementation (abstractlazyinitializer.java:174)
At Org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke (javassistlazyinitializer.java:190)
At Com.test.model.user_$$_javassist_0.tostring (User_$$_javassist_0.java)
At Java.lang.String.valueOf (string.java:2854)
At Java.io.PrintStream.println (printstream.java:821)
At Com.test.juniTest.otherTest.findUserById (othertest.java:91)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:606)
At Org.junit.runners.model.frameworkmethod$1.runreflectivecall (frameworkmethod.java:47)
At Org.junit.internal.runners.model.ReflectiveCallable.run (reflectivecallable.java:12)
At org.junit.runners.model.FrameworkMethod.invokeExplosively (frameworkmethod.java:44)
At Org.junit.internal.runners.statements.InvokeMethod.evaluate (invokemethod.java:17)
At Org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate ( runbeforetestmethodcallbacks.java:74)
At Org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate ( RUNAFTERTESTMETHODCALLBACKS.JAVA:83)
At Org.springframework.test.context.junit4.statements.SpringRepeat.evaluate (springrepeat.java:72)
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (springjunit4classrunner.java:231)
At Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:50)
At Org.junit.runners.parentrunner$3.run (parentrunner.java:238)
At Org.junit.runners.parentrunner$1.schedule (parentrunner.java:63)
At Org.junit.runners.ParentRunner.runChildren (parentrunner.java:236)
At org.junit.runners.parentrunner.access$000 (parentrunner.java:53)
At Org.junit.runners.parentrunner$2.evaluate (parentrunner.java:229)
At Org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate ( RUNBEFORETESTCLASSCALLBACKS.JAVA:61)
At Org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate ( RUNAFTERTESTCLASSCALLBACKS.JAVA:71)
At Org.junit.runners.ParentRunner.run (parentrunner.java:309)
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (springjunit4classrunner.java:174)
At Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (junit4testreference.java:50)
At Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:459)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:675)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:382)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:192)

Analysis: This error is common and literal meaning cannot be initialized because the session is closed. The simple understanding is that you use lazy=true so that hibernate does not look up the associated object when it is tuning data from the database, but instead saves a fetch-worthy method that, when you use the getxxx () call, Hiberante will use this saved method to fetch data from the database. And often we use GETXXX () in the JSP page to prepare to display the data, the session has already been closed in the DAO, if the entity bean is injected through the annotation mode (configuration mode is no longer discussed in this article), the workaround is as follows:

Test class:

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations={"File:webcontent/web-inf/applicationcontext2.xml"})     Public classothertest{@AutowiredPrivateHibernatetemplate hibernatetemplate;

//Set method must be setup Public voidsethibernatetemplate (hibernatetemplate hibernatetemplate) { This. hibernatetemplate =hibernatetemplate; } //specify user by ID query//Note: The user is required to set @org.hibernate.annotations.proxy (lazy = false)@Test Public voidFinduserbyid () {User u= Hibernatetemplate.load (User.class, 31); SYSTEM.OUT.PRINTLN (u);//User [id=31, Username= Wang Dada 3, password=111, [email protected]] } }

Entity Bean

@Entity @table (name= "T_user") @org. Hibernate.annotations.Proxy (Lazy=false)//Set Load Now Public classUser {Private intID; PrivateString username; PrivateString password; PrivateString Email; @GeneratedValue @Id Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; } @NotEmpty (Message= "User name cannot be empty")     PublicString GetUserName () {returnusername; }         Public voidSetusername (String username) { This. Username =username; } @Size (min=1,max=10,message= "The length of the password should be between 1 and 10")     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; } @Email (Message= "Mailbox format is incorrect")     PublicString Getemail () {returnemail; }     Public voidsetemail (String email) { This. email =email; }     PublicUser (string Username, string password, string email) {Super();  This. Username =username;  This. Password =password;  This. email =email; }         PublicUser () {} @Override PublicString toString () {return"User [id=" + ID + ", username=" + Username + ", password=" + password + ", email=" + email + "]"; }        }

Attached: If it is a configuration mode, the workaround reference is below:

http://blog.csdn.net/s_good/article/details/7411642

Acknowledgement: http://bbs.csdn.net/topics/390522138

@Entity Set Entity lazy = False

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.