A simple example:
Java code
@ SuppressWarnings ("unchecked ")
Public void searchByPropertys (){
Session session = this. getSession ();
Criteria crit = session. createCriteria (Conft. class );
List <Conft> list = crit. list ();
For (Conft c: list ){
System. out. println (c. getId ());
}
}
The standard query API is eventually translated into SQL and processed by the database. The java. util. List object is returned.
How to add conditions? See the following code:
Java code
Public void searchByPropertys (){
Session session = this. getSession ();
Criteria crit = session. createCriteria (Conft. class );
Crit. add (Restrictions. eq ("id", 2); // =
Crit. add (Restrictions. ne ("id", 2 ));//! =
Crit. add (Restrictions. lt ("id", 2); // <
Crit. add (Restrictions. gt ("id", 2); //>
Crit. add (Restrictions. le ("id", 2); // <=
Crit. add (Restrictions. ge ("id", 2); //> =
Crit. add (Restrictions. in ("id", new String [] {"2"}); // in
Crit. add (Restrictions. like ("id", "% 2%"); // like
Crit. add (Restrictions. like ("id", "2", MatchMode. ANYWHERE); // % x %
Crit. add (Restrictions. like ("id", "2", MatchMode. START); // x %
Crit. add (Restrictions. like ("id", "2", MatchMode. END); // % x
Crit. add (Restrictions. like ("id", "2", MatchMode. EXACT); // x
List <Conft> list = crit. list ();
For (Conft c: list ){
System. out. println (c. getId ());
}
}
The method and meaning of adding conditions are written after the code.
If there is an and or OR relationship, you can use Conjunction (AND) and Disjunction (or )!
The following is an example:
Java code
Public void searchByPropertys (){
Session session = this. getSession ();
Criteria crit = session. createCriteria (Conft. class );
// Create conditions
Criterion a = Restrictions. gt ("id", 2); //>
Criterion B = Restrictions. lt ("id", 2); // <
Criterion c = Restrictions. like ("id", "2", MatchMode. ANYWHERE );
// And relationship
Conjunction conjunction = Restrictions. conjunction ();
Conjunction. add ();
Conjunction. add (B );
// Or relationship
Disjunction disjunction = Restrictions. disjunction ();
Disjunction. add (conjunction );
Disjunction. add (c );
// Add query Conditions
Crit. add (disjunction );
List <Conft> list = crit. list ();
For (Conft conft: list ){
System. out. println (conft. getId ());
}
}
A AND B are in the AND relationship, while a AND B are in the OR relationship with c after they are combined as conditions!
If you look at his SQL statement, it will help you understand:
Java code
(Where (a>? And B <?) Or c like ?)
In addition, you can use sqlRestriction to directly splice SQL statements.
Java code
Public User getUserById (int pk ){
Session session = this. getSession ();
Criteria crit = session. createCriteria (User. class );
Crit. add (Restrictions. sqlRestriction ("{alias}. id = 2 "));
List <User> list = crit. list ();
Return (User) list. get (0 );
}
Note that {alias} is the table name, which does not need to be modified. Hibernate will automatically replace it when generating SQL!
In the source code, you can see:
Java code
Public static Criterion sqlRestriction (String SQL, Object values [], Type types [])
{
Return new SQLCriterion (SQL, values, types );
}
Public static Criterion sqlRestriction (String SQL, Object value, Type type)
{
Return new SQLCriterion (SQL, new Object [] {
Value
}, New Type [] {
Type
});
}
Public static Criterion sqlRestriction (String SQL)
{
Return new SQLCriterion (SQL, ArrayHelper. EMPTY_OBJECT_ARRAY, ArrayHelper. EMPTY_TYPE_ARRAY );
}
That is to say, this method has three calling methods: Writing SQL directly, if your SQL has? As a placeholder, you can set the following two parameters. The second parameter corresponds? , The third is the type of the parameter field!
If there are multiple? The second and third parameters are passed in the array format!
Author: dylove98