Hibernate configuration --- Search Policy

Source: Internet
Author: User

Hibernate is a persistence layer framework that does not rely on any container and supports ing objects to database tables, ORM.

Supports addition, deletion, modification, and query operations during development and encapsulates basic JDBC operations. You can configure hibernate. cfg. xml to conveniently switch databases. Implements system and database solutions.

Supports database connection pools to prevent slow system response and system crashes due to high concurrency. When multiple users access the application at the same time, each request will open up a connection object, which will consume time when it is enabled or disabled. If there is no access restriction to the database, the system response will become slower and slower, if the memory is not recycled in time, the system crashes. Hibernate is commonly used as the c3p0 connection pool, which is configured in hibernate. cfg. xml.

Search policy:

Multiple-to-one Configuration Policy:

In the allow-to-one element, you can specify outer-join = "true" lazy = "false" and use the method of urgently performing external join retrieval, use a statement to query the related end. The default value is "Search now", and the associated tables of the statement query will be issued separately. Lazy can only be set to proxy, no-proxy, or false. If you want to delay loading one end, you need to configure outer-join = "proxy" in sequence-to-one and set lazy = true on the class attribute of one-to-sequence end.

You can specify outer-join = "true" lazy = "false" on the set in the one-to-outer element. This element is searched immediately by default by pressing the left outer join.

If lazy is set to true, latency loading is applied regardless of the value of outer-join.

One-to-one association delayed loading Configuration:


Seal class at one end:

<One-to-one name = "sealData"
Class = "com. indigopacific. iessportal. persistent. SealData"
Cascade = "save-update" lazy = "proxy" constrained = "true"/>

SealData on the other end:

<Class name = "com. indigopacific. iessportal. persistent. SealData"
Table = "tsealdata" dynamic-update = "true" dynamic-insert = "true"
Select-before-update = "false" lazy = "true">

Check the red configuration to delay sealData loading when querying seal.

Delayed loading can be mainly used in:

Hibernate3.x. lazy is enabled by default.
Hibernate Lazy policy usage:
<Class> label, which can be set to true or false.
<Property> tag, which can be set to true or false.
<Set> <list> label, which can be set to true/false/extra.
<One-to-one> <allow-to-one> single-ended Association. Optional values: false/proxy/noproxy
Load supports lazy and get, regardless of whether the persistence class is set or not.

(1) class label
<Class name = "className" lazy = "true/alse">
The default value is true.
Lazy on class only works for common attributes and does not work for the set and <one-to-one> <allow-to-one>
That is, if a class contains Set member variables and the member variables that need to be mapped using <one-to-one> <allow-to-one>, even if lazy is set to false in the class, they will not load it before use. Lazy needs to be set separately, that is, the other two lazy usage.
If an object is loaded using load but is not used, the session is closed if no SQL statement is issued. If this object is used again, LazyInitalizationException is thrown. Because the object is not loaded, the lazy policy is used, but a proxy subclass of the class is loaded.

(2) Set
<Set name = "name" lazy = "true/false/extra">
The default value is true.
If the default value is true, the entire Set is queried as long as the set object is used.
If it is false, Lazy is not used. When you query the object to which Lazy belongs, set will be queried. Class enhancement tools are required.
Extra is more intelligent. Different SQL statements are generated based on the queried content. Higher efficiency.

(3) single-ended Association
<Your-to-one name = "name" lazy = "false/proxy/noproxy">
The default value is proxy.
False: Do not use Lazy.
Noproxy: Class enhancement tools required

(4) property
<Property name = "name" lazy = "true/false"/>
Default Value: false.
True: it is rarely used. If the internal capacity of a field is large, you can set it to true to load it when it is actually used. Can improve efficiency ., However, the hibernate toolkit, org. hibernate. tool. instrument. cglib uses Ant to enhance the compiled class file. In fact, it adds some cglib code to the class file, so that hibernate can determine whether the attribute needs to be loaded immediately at runtime, otherwise, the load will be delayed. To achieve this goal, compilation and enhancement are required each time, which is not conducive to project maintenance. The method is as follows: extract large fields into a table, and establish a one-to-one association with the basic table, depending on the business, when querying, you can use Hibernate's latency policy to achieve lazy loading.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.