Hibernate's retrieval strategy

Source: Internet
Author: User
Tags set set

Hibernate's search strategy is divided into three types

1 class-level retrieval strategy, operating level in a single table, without implicating other tables

The <class> element's Lazy property is true or false, and the Get () method of the Session and the list () method of the Query always use the immediate retrieval policy at the class level if <class> The lazy property of the element is true

21-to-many and many-to-many search strategies (in fact, the set tag of the lazy, batch-size, fetch properties for the needs of the setting)

< Set > elements to configure a one-to-many association and many-to-many association relationships. (Customer (1), order (n))
< Set > the element has the lazy and fetch properties lazy: The primary decision is when the order collection is initialized. Whether it is initialized when the Customer object is loaded, or when the program accesses the Orders collection
---------------Set's Lazy property------------------        //1.1-n or N-n collection properties default to lazy-loaded retrieval policies.        2. You can modify the default retrieval policy by setting the lazy property of the set. The default is True        //is not recommended to be set to  false.        3. Lazy can also be set to extra. Enhanced deferred retrieval. This value will delay the collection initialization whenever possible! (The collection object can not be loaded without loading)
You can use the Initialize () method to force the initialization of a collection object (which is generally recommended) (most of the time the default value is available)
 <set> Element's Batch-size property:  set to initialize set set once   .  is used to set the number of bulk searches for a deferred retrieval policy or an immediate retrieval policy. Bulk retrieval can reduce the number of SELECT statements and improve the performance of deferred or immediate retrieval operations. 
Note The initialized collection is not an associated object:
Example:
  @Test  public  void   Testsetbatchsize () {List  <Customer> customers = Session.crea                Tequery ("from Customer"  for   (Customer customer:customers) {
     if  (customer.getorders ()! = null  Span style= "color: #000000;"                >) System.out.println (Customer.getcustomername ());                System.out.println (Customer.getorders (). iterator (). Next (). Getordername ());        System.out.println (Customer.getorders (). GetClass ()); }

run the above code to know that the initialization is just a list<> collection, but not initialize the associated object order, So the batch-size initialized value is the object of the current method Set<> collection, not the object being associated (order)

Fetch property of the Set collection: Determines the initialization of the Orders collection (the associated object
1. The default value is select. Initialize the set element in a normal way--lazy loading
2. You can take a value of subselect. Initializes all set sets and all associated objects by means of a subquery.
3. If the value is a join. the time when the collection and the associated object are initialized, that is, lazy invalidation
3.1 When loading an object at one end of 1, it also uses an urgent left outer join (querying with a left outer link and initializing the collection property) to retrieve the collection property of the end of N 3.2 ignores the lazy property.
3.3 HQL (that is, Hibernate's query database statement (unlike SQL) query ignores the value of Fetch=join

3 multi-pair and one-to-two correlation strategies (lazy, Fetch, (both of which belong to Many-to-one and one-to-one) batch-size (which belong to Class) are set)

The multiple-to-one-to-single association corresponds to Many-to-one and one-to-one, so a multi-pair and one-to-one association policy is also set for the properties of the two tags

Policy setting: in all associated relationships, all associated objects are the default load policy is lazy loading (so many-to-one and a-to-a-to-be associated object is no exception)

        1. The lazy value is a proxy and false represents the corresponding attribute with deferred retrieval and immediate retrieval of        2. Fetch value is join, which indicates that an attribute that initializes a side of n associated 1 with an urgent left outer join ignores the lazy property .         3. Batch-size, this property needs to be set at the 1 end of the class         element:<name= "Customer"  table  = "CUSTOMERS"  lazy= "true"  batch-size= "5">         

Hibernate's retrieval strategy

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.