In Atitit. Hibernate, Criteria uses summary and join queries and queries o9o and hibernatecriteria by sub-objects.

Source: Internet
Author: User

In Atitit. Hibernate, Criteria uses summary and join queries and queries o9o and hibernatecriteria by sub-objects.

In Atitit. Hibernate, Criteria uses summary and association query and queries o9o by sub-Object

 

1. Criteria, Criterion, 1

<2. Configure the Lei Association for the main object blackhead, 3: 1

3. Criteria blackhead configuration association query... 2

4. Ati Meata configuration Association @ CriteriaRelt 2

5. Associate createCriteria () 3

6. Use outline 4 for Criteria

7. Reference: 5

 

 

1. Criteria, Criterion ,,

Criteria is a more Object-Oriented Query Method than hql. Criteria can use Criterion and Projection to set query conditions. You can set the FetchMode (the mode of joint query capture) and the sorting mode. Criteria can also set FlushModel (the method of flushing sessions) and LockMode (the database lock mode ).

2. Configure Lei Association for the main object blackhead, and 3

Public class GvPlayRecord

Equipment eq;

/**

// Attilax old wow's paw 5_u_8 o9n

* @ Return the eq

*/

@ ManyToOne

@ NotFound (action = NotFoundAction. IGNORE)

@ JoinColumn (name = "equipment_id", insertable = false, updatable = false)

@ RemoteProperty

Public Equipment getEq (){

Return eq;

}

Criteria c = new BaseSvs (). getSession (). createCriteria (GvPlayRecord. class );

SimpleExpression se = Restrictions. eq ("eq. dpt. groupid", 114 );

// C. add (se );

List list = c. list ();

 

Iii. New eq. dpt. groupid attribute... it seems that the configuration of direct blackhead does not take effect ..

 

Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://blog.csdn.net/attilax

 

 

 

3. Criteria blacklist configuration association query...

Criteria c = new BaseSvs (). getSession (). createCriteria (GvPlayRecord. class );

Criteria eqCri = c. createCriteria ("eq ");

EqCri. add (Restrictions. eq ("departId", 117 ));

 

Middle Line lan...

 

4. Ati Meata configuration Association @ CriteriaRelt

 

@ CriteriaRelt (Signature = "departId ")

Equipment eq;

 

 

 

Framework processing code

// Yash condition process. def

SqlAti_Criteria SQL = new SqlAti_Criteria ();

SQL. reqMap = QueryPropertyssMap;

SQL. Criteria = c;

SQL. setCls (cls). addWhereExp (alias );

 

@ SuppressWarnings ("all") public SqlAti addWhereExp (Field alias ){

CriteriaRelt an = criteria. getAnnotation (CriteriaRelt. class );

If (! = Null)

{

Criteria crSub = this. Criteria. createCriteria (Criteria. getName ());

CrSub. add (Restrictions. eq (an. Equals (), this. reqMap. get (an. Equals ())));

}

 

Return this;

}

5. Associate createCriteria ()

4.

5.
You can use createCriteria () to easily establish constraints between correlated entities.
 
List cats = sess. createCriteria (Cat. class)
. Add (Restrictions. like ("name", "F % ")
. CreateCriteria ("kittens ")
. Add (Restrictions. like ("name", "F % ")
. List ();


Note that the second createCriteria () returns a new Criteria instance that references elements in the kittens set.
Next, the replacement form is also very useful in some cases.
 
List cats = sess. createCriteria (Cat. class)
. CreateAlias ("kittens", "kt ")
. CreateAlias ("mate", "mt ")
. Add (Restrictions. eqProperty ("kt. name", "mt. name "))
. List ();


(CreateAlias () does not create a new Criteria instance .)
The kittens set returned by the previous two queries saved by the Cat instance is not pre-filtered by the condition. If you want to only get

For eligible kittens, you must use returnMaps ().
 
List cats = sess. createCriteria (Cat. class)
. CreateCriteria ("kittens", "kt ")
. Add (Restrictions. eq ("name", "F % "))
. ReturnMaps ()
. List ();
Iterator iter = cats. iterator ();
While (iter. hasNext ()){
Map map = (Map) iter. next ();
Cat cat = (Cat) map. get (Criteria. ROOT_ALIAS );
Cat kitten = (Cat) map. get ("kt ");
}

 

 

6. Use outline for Criteria

5. Dynamic Association crawling

6. query example
The org. hibernate. criterion. Example class allows you to construct a conditional query using a given instance.

7. 7. Projections, aggregation, and grouping)

8. 8. Offline (detached) queries and subqueries

 

7. Reference ::

Full use of Criteria in Hibernate-LifeNote-BlogJava.htm


How does hibernate Criteria query sub-object Association,

First:
Criteria criteria = hibernateTemplate. getSessionFactory (). getCurrentSession (). createCriteria (Dept. class );
Criteria. add (Restrictions. like ("name", "% aa % "));
Second:
Criteria criteria = hibernateTemplate. getSessionFactory (). getCurrentSession (). createCriteria (Dept. class );
Criteria senduser = criteria. createCriteria ("a"); // This small a is the attribute name associated with A. class in Dept.
Criteria. add (Restrictions. like ("name", "% AA % "));
Last return
Return criteria. list ();

How can I query the object field of Criteria in Hibernate?

After arriving at Hibernate3.0, the objects of the Association relationship use delayed loading by default, for example, when <one-to-least. however, I specified lazy = "true" When ing the <one-to-one and <others-to-one relationships, but when querying objects, I only want to query one object, the <one-to-one and <others-to-one objects associated with this object will still be queried together, resulting in a great waste of performance. when the lazy attribute is not specified, the objects associated with <role-to-one will delay loading, which makes me very confused and thinks it is a Hibernate bug.
Search for information online, saying that in order to delay loading the objects associated with <one-to-one, <allow-to-one, you need to set the associated object <class name = "" lazy = "true". I also did this, but it still has no effect.
After carefully reading The Hibernate manual, we can find that the objects associated with <one-to-one and <others-to-one are delayed, in addition to specifying lazy = "true", you also need to enhance the runtime bytecode. This step is skipped, so delayed loading is ineffective. by default, the lazy attribute of <one-to-one and <others-to-one is "proxy" instead of "true "! Therefore, if the default value of lazy is used directly, loading can be delayed.
To sum up, the default attribute of <role-to-one is lazy = "proxy". By default, loading is delayed. after lazy = "true" is specified, the bitcode must be increased during the runtime, and delayed loading will be effective.
<One-to-one is a little more complex, and delayed loading is also limited by the constrained attribute. constrained = "false" indicates that the constraints on the object and the object to be associated are not mandatory. That is, when an object exists, the object associated with the object through <one-to-one may exist, or it may not exist. When you query an object, Hibernate always initiates a query check to check whether the object associated with <one-to-one exists, at this time, you can query the entities associated with one-to-one. Therefore, in the <one-to-one relationship, if constrained = "false ", it will always immediately load the associated entities.
If constrained = "true" and lazy = "proxy" (default), loading can be delayed.
If constrained = "true" and lazy = "true", the delay loading takes effect only when the bytecode increases during the runtime.
If the data size is too large, it is still necessary. This can also be done, but the returned result is not an object, but a field array.
Hibernate calls this projection Criteria criteria = session. createCriteria (User. class );
ProjectionList proList = Projections. projectionList (); // sets the proList of the projection set. add (Projections. groupProperty ("userName"); proList. add (Projections. groupProp ...... remaining full text>

Related Article

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.