How hibernate is retrieved

Source: Internet
Author: User

how hibernate is retrieved
How to retrieve ( Query method )
Navigation Object Graph retrieval method: Navigating to other objects based on objects that have already been loaded
Customer customer = (customer) session.get (customer.class,1);
customer.getorders ();//obtain a customer's order
oid Retrieval Method: Retrieving objects by the OID of the object
get ()/load (); method to retrieve.
HQL Retrieval Method: Using object-oriented HQL query Language
query query = session.createquery ("HQL");
QBC Retrieval Method: Use the QBC (query by Criteria) API to retrieve objects. This API encapsulates query statements based on string literals and provides a more object-oriented query interface.
Criteria = Session.createcriteria (customer.class);
local SQL Retrieval method: SQL query statement using local database
sqlquery query = Session.createsqlquery ("SQL");
HQL
hql:hibernate Query Language
Features: Object-oriented query, support method chain programming
1. Querying All Records  

list<customer> list = Session.createquery ("from Customer"). List ();  for (Customer customer:list) {System.out.println (customer);}

  2. Use aliases for queries

// using aliases, alias as can be omitted List<customer> list =session.createquery ("from Customer C"). List (); SYSTEM.OUT.PRINTLN (list); // using aliases: with parameters list<Customer> list = Session.createquery ("from Customer as C where c.cname =?"). SetString (0, "Xiao Shen"). List (); SYSTEM.OUT.PRINTLN (list); // The select * from customer notation is not supported. You can write a select alias from the customer as alias; list<customer> list = Session.createquery ("Select C from Customer C"). List (); SYSTEM.OUT.PRINTLN (list);

  3. Sorting

list<customer> list = session.createquery ("from Customer C order by c.id Desc"). List ();  for (Customer customer:list) {System.out.println (customer);}

  4. Paging Query

Query query = Session.createquery ("from Order"); Query.setfirstresult(10); Query.setmaxresults ); List<Order> list = query.list ();  for (Order order:list) {System.out.println (order);}

  5. Single Object query

Customer customer = (Customer) Session.createquery ("from customer where CNAME =?") ). SetString (0, "Xiaoming"). Uniqueresult (); SYSTEM.OUT.PRINTLN (customer);

  6. Parameter binding

//1. Bind using the Sign methodQuery query = Session.createquery ("from Customer where cname =?")); Query.setstring (0, "Xiao Shen"); List<Customer> list =query.list (); SYSTEM.OUT.PRINTLN (list); Query Query= Session.createquery ("from Customer where cname =?") and CID =? "); Query.setstring (0, "Xiao Shen"); Query.setinteger (1,3); List<Customer> list =query.list (); SYSTEM.OUT.PRINTLN (list);
//2. Binding with a nameQuery query = Session.createquery ("From Customer where Cname=:name and Cid=:id"); Query.setstring ("Name", "Xiao Shen"); Query.setinteger ("ID", 3); List<Customer> list =query.list (); SYSTEM.OUT.PRINTLN (list);//3. Binding Entitieslist<order> list =Session.createquery ("From Order o where O.customer =?"). Setentity (0, customer). List (); for(Order order:list) {System.out.println (order);}

  7. Projection operation

// Query the name of the customer list<object> list = Session.createquery ("Select C.cname from Customer C"). List (); SYSTEM.OUT.PRINTLN (list); // Check two properties list<object[]> list = Session.createquery ("Select C.cid,c.cname from Customer C"). List ();  for (object[] objects:list) {System.out.println (Arrays.tostring (objects));} // need to provide the appropriate construction method list<customer> list = Session.createquery ("Select New Customer (CNAME) from Customer"). List (); SYSTEM.OUT.PRINTLN (list);

  8. Fuzzy Query

Query query = Session.createquery ("From Customer where cname is like?") ); Query.setparameter (0, "small%"); List<Customer> list = query.list (); SYSTEM.OUT.PRINTLN (list);    

  9. Aggregation function

Long Count = (long) session.createquery ("SELECT count (*) from Order"). Uniqueresult (); System.out.println (count);    

  10. Name the query (configure the query statement in the configuration file)

list<customer> list = Session.getnamedquery ("FindAll"). List (); SYSTEM.OUT.PRINTLN (list);

SQL Multi-table query (connection)  
cross-connect (less used):select * from a a B
Inner Connection ( Natural connection ):SELECT * from a INNER join B on a. field = B. field;
implicit inner joins:select * from A, A where A. field = B. field;
External Connection
left outer connection:SELECT * from a ieft outer join B on A. field = B. field;
right outer connection:SELECT * from a-outer join B on a. field = B. field;
hql Multi-table Query  
cross-Connect, inner connection, implicit inner connection, urgent inner connection, left outer connection, urgent left outer connection, right outer connection
Implicit in-connection (learn)
createquery ("from Order o where o.customer.name like ' t% '");
equivalent to: from Order o join O.customer C where c.name like ' t% '
HQL and urgent inner connection differences (other similar):
INNER JOIN query: Encapsulates the data in a list<object[]>.
Urgent Internal connection: encapsulates the data in a list<customer>. But the urgency of the connection, get a duplicate record, you need to use distinct weight.

//Internal connection query:Query query = Session.createquery ("from Customer C inner join C.orders"); List<Object[]> list =query.list (); for(object[] objects:list) {Customer C= (Customer) objects[0]; Order o= (Order) objects[1]; System.out.println (C.getcid ()+ "" "+c.getcname () +" "+c.getage ()); System.out.println (o.getoid ()+" "+o.getaddr ());}//Urgent Internal connection: Using a keyword fetch (HQL)Query query = session.createquery ("SELECT DISTINCT C from Customer C INNER join fetch c.orders"); List<Customer> list =query.list (); for(Customer customer:list) {System.out.println (customer);}

QBC
  1. Querying All Records

list<customer> list = Session.createcriteria (Customer.  Class). List ();  for (Customer customer:list) {System.out.println (customer);}

  2. Sorting

list<customer> list = Session.createcriteria (Customer.  Class). AddOrder (Org.hibernate.criterion.Order.desc ("id")). List ();  for (Customer customer:list) {System.out.println (customer);}

  3. Paging

Criteria = Session.createcriteria (Order.  Class); Criteria.setfirstresult ();criteria.setmaxresults (ten); List<Order> list = criteria.list ();  for (Order order:list) {System.out.println (order);}

  4. Get a single object

Customer customer = (customer) Session.createcriteria (customer).  Class). Add (Restrictions.eq ("CNAME", "xiaoming")). Uniqueresult (); SYSTEM.OUT.PRINTLN (customer);

  5. Queries with parameters

list<customer> list = Session.createcriteria (Customer.  Class). Add (Restrictions.eq ("CNAME", "xiaoming")). List (); SYSTEM.OUT.PRINTLN (list); List<Customer> list = Session.createcriteria (Customer.  Class). Add (Restrictions.eq ("CNAME", "xiaoming")). Add (Restrictions.eq ("CID", 2)). List (); SYSTEM.OUT.PRINTLN (list);

  6. Fuzzy query

Criteria = Session.createcriteria (Customer.  Class); Criteria.add (Restrictions.like ("CNAME", "large%")); List<Customer> list = criteria.list (); SYSTEM.OUT.PRINTLN (list);

Sql

list<object[]> list = Session.createsqlquery ("SELECT * from Customer"). List ();  for (object[] objects:list) {System.out.println (Arrays.tostring (objects));} List<Customer> list = Session.createsqlquery ("SELECT * from Customer"). Addentity (Customer. class ). List ();  for (Customer customer:list) {System.out.println (customer);}

Offline query
  

@Test/** Offline conditional query*/ Public voidDemo () {//encapsulation of the Web tierDetachedcriteria criteria = Detachedcriteria.forclass (Customer.class); Criteria.add (Restrictions.eq ("CNAME", "xiaoming")); Criteria.add (Restrictions.eq ("CID", 2)); //pass to the DAO layerSession session =hibernateutils.opensession (); Transaction TX=session.begintransaction (); Criteria C1=Criteria.getexecutablecriteria (session); List<Customer> list =c1.list ();  SYSTEM.OUT.PRINTLN (list);  Tx.commit (); Session.close ();}

How hibernate is retrieved

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.