Hibernate.initialize (OBJ) usage

Source: Internet
Author: User

Guide:
When using hibernate for persistence, it is sometimes necessary to dynamically change the loading of objects, such as lazy=true in the edit page, and on the browse page lazy=false, so that you can control it where it needs to be lazy. and the lazy attribute in the configuration file is globally controlled, how to handle it?
When the lazy property of an element or element is true, when load () or get () or find () loads these objects, hibernate does not immediately produce any SELECT statements, but produces an instance of the Obj proxy class. The SELECT statement loads the object from the database only if the session is running OBJ.GETXXX () without shutting down, and if no obj.getxxx () method is run and the session is closed, obj is in a Free State, When you run the Obj.getxxx () method again, hibernate throws "Could not initialize Proxy-the owning Session is Closeed" exception, which means that the OBJ proxy class instance cannot be initialized. However, if you want to close the session without calling the Obj.getxxx () method before the session closes, call Hibernate.initialize (OBJ) just before the session closes. or Hibernate.initialize (Obj.getxxx ()), the Initialize () static method of the Net.sf.hibernate.Hibernate class is used to display the initialization proxy class instance within the session scope.
In the configuration file can be used Lazy=true, in the program can be forced to load the method Hibernate.initialize (Object proxy) method force loading, which is equivalent to dynamic change to Lazy=false.
But one thing to be aware of when using it is that the proxy is a persistent object's associated object property, such as a entity, and you want to check out the associated entity B for a, then write Hibernate.initialize (A.B).

Hibernate.initialize (OBJ) usage

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.