- Pre-compiled statements from the previous section hql
Pre-compilation in Hibernate is similar to the process of spring precompilation.
1: Using the standard?
2: Using named parameters
2.1: Set with Name one by one.
2.2: Use Map (the value of key equals the name of the parameter)
2.3: Using the bean (the name of the property equals the name of the parameter)
Case one:Query_hql_prepared.java
PackageAction;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.hibernate.Query;Importorg.hibernate.Session;Importorg.hibernate.Transaction;ImportBean. User;Importutil. Hibernateutil;/*** Preprocessing instructions for this type of test HQL * * Pre-compilation in Hibernate is similar to the process of spring precompilation. * * 1: Use standard? * * 2: Use named parameter * * 2.1: Set with Name one by one. * * 2.2: Use the map (the value of key equals the name of the parameter) * * 2.3: Use the bean (the name of the property equals the name of the parameter) * *@authorhalf a lemon, **/ Public classquery_hql_prepared { Public Static voidMain (string[] args) {//Query_hql_prepared.one ();//Use the standard? //query_hql_prepared.two ();//Use names to set them one by one . //Query_hql_prepared.three ();//use Map (the value of key equals the name of the parameter)Query_hql_prepared.four ();//using the bean (the name of the property equals the name of the parameter) } /** Use the standard? */ Private Static voidOne () {Session session=NULL; Transaction Tran=NULL; Query Query=NULL; String hql= ""; Try{Session=hibernateutil.getsession (); Tran=session.begintransaction (); HQL= "Select u from User u where userid<=?" and userid>=? and username like? "; /*** The index of HQL is starting from 0*/Query=session.createquery (HQL); Query.setparameter (0, 6); Query.setparameter (1, 1); Query.setparameter (2, "%user%"); List<User> userlist =query.list (); for(User user:userlist) {System.out.println ("Username=" +User.getusername ()+ "\ t userid=" +User.getuserid ()); } tran.commit (); } Catch(Exception e) {tran.rollback (); E.printstacktrace (); } finally{hibernateutil.closesession (); } } /*** Use names to set them individually*/ Private Static void() {Session session=NULL; Query Query=NULL; Transaction Tran=NULL; String SQL= ""; Try{Session=hibernateutil.getsession (); Tran=session.begintransaction (); SQL= "Select u from User u where username like:username and userid>=: Userid_begin and userid<=: Userid_end"; Query=session.createquery (SQL); Query.setstring ("username", "%user%"); Query.setinteger ("Userid_begin", 3); Query.setinteger ("Userid_end", 5); List<User> userlist =query.list (); for(User user:userlist) {System.out.println ("Username=" +User.getusername ()+ "\ t userid=" +User.getuserid ()); } tran.commit (); } Catch(Exception e) {e.printstacktrace (); Tran.rollback (); } finally{hibernateutil.closesession (); } } /*** Use Map (the value of key equals the name of the parameter)*/ Private Static voidThree () {Session session=NULL; Transaction Tran=NULL; Query Query=NULL; String SQL= ""; Try{Session=hibernateutil.getsession (); Tran=session.begintransaction (); SQL= "Select u from User u where userid>=: Userid_begin and userid<=: Userid_end and Username Like:username"; Query=session.createquery (SQL); Map<String,Object> sqlmap=NewHashmap<string,object>(); Sqlmap.put ("Userid_begin", 3); Sqlmap.put ("Userid_end", 5); Sqlmap.put ("username", "%user%"); Query.setproperties (SQLMAP); List<User> userlist =query.list (); for(User user:userlist) {System.out.println ("Username=" +User.getusername ()+ "\ t userid=" +User.getuserid ()); } tran.commit (); } Catch(Exception e) {e.printstacktrace (); Tran.rollback (); } finally{hibernateutil.closesession (); } } /*** Use Bean (the name of the property equals the name of the parameter)*/ Private Static voidFour () {Session session=NULL; Query Query=NULL; Transaction Tran=NULL; String SQL=""; Try{Session=hibernateutil.getsession (); Tran=session.begintransaction (); SQL= "Select u from User u where username like:username and userid>=: Userid_begin and userid<=: Userid_end"; User User=NewUser (); User.setusername ("%user%"); User.setuserid_begin (3);//The Userid_begin and Userid_end properties must be in the User bean, with the Get and set methodsUser.setuserid_end (5); Query=session.createquery (SQL); Query.setproperties (user); List<User> userlist =query.list (); for(User use1r:userlist) {System.out.println ("Username=" +Use1r.getusername ()+ "\ t userid=" +Use1r.getuserid ()); } tran.commit (); } Catch(Exception e) {e.printstacktrace (); Tran.rollback (); }finally{hibernateutil.closesession (); } }}
(v) Hibernate additions and deletions change operation (2)