Projection query--Filter some fields
The returned list collection element is object[]
Query query = Session.createquery ("Select C.cname, c.csex from Customer C"); List List = Query.list (); Iterator iter = List.iterator (); object[] Objs = Null;while (Iter.hasnext ()) {Objs = (object[]) ITER . Next (); for (int i = 0; i < objs.length; i + +) {System.out.println (objs[i]); System.out.println ("-------------------");
Paging Query
The returned list collection element is an object
Query query = session.createquery ("Select C from the Customer C left join C.orders o where c.id=1"); Query.setmaxresults (2);//sets the maximum number of records to query per query, which is simply the page size query.setfirstresult (2);//sets the starting position of each query, Iterator iter = Query.list (). iterator (); while (Iter.hasnext ()) {System.out.println ((Customer) Iter.next ()); }
Batch Processing
@Testpublic void Testbatchdealwith () {for (int i = 0; I <10000; i + +) {customer customer=new customer (); Customer.setcusto Mername ("Jeremy" +i); Session.save (customer); if (i==20| | I==40) {System.out.println ("--------------------------");}}}
Because hibernate caches all persistent objects at the session level by default, and the final application fails and occurs outofmemoryexception somewhere around 50,000 records. If you are using batch processing with Hibernate to resolve this issue.
To use the bulk processing feature, first set hibernate.jdbc.batch_size to batch size several either at 20 or 50 depending on the size of the object. This tells the container to insert a batch of hibernate into each x row. In order to implement this in the code, we need to make a little change as follows:
@Testpublic void Testbatchdealwith () {for (int i = 0; I <10000; i + +) {customer customer=new customer (); Customer.setcustomername ("Jeremy" +i); Session.save (customer); if (i==50) {Session.flush (); Session.clear ();}} }
<name= "hibernate.jdbc.batch_size"></Property >
This will give you a good boost in speed.
Hibernate's batch processing and paging technology, projection technology