The following issues were identified in MySQL with the Hibernate test UPDATE statement today:
The UPDATE statement is not going to work;
The table is as follows:
CREATE TABLE Student
(SID int Primary KEY,
sname varchar () NOT NULL,
Ssex char (2) NOT NULL,
Sdept varchar (TEN) is not NULL,
Sage Int,
saddress varchar (45)
);
The UPDATE statement is as follows:
string[] params = new string[] {"20", "Cheng Long"};
Hibernateutil.executeupdate (
"Update Student s set s.sage=?" Where s.sname=? ", params);
PackageCom.huml.util;Importjava.util.ArrayList;Importjava.util.Arrays;Importorg.hibernate.Transaction;ImportOrg.hibernate.Query;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration; Public classHibernateutil {Private Static Finalsessionfactory SF; Private StaticThreadlocal<session> ThreadLocal =NewThreadlocal<session>(); Static{SF=NewConfiguration (). Configure (). Buildsessionfactory (); } PrivateHibernateutil () {} Public StaticSession opensession () {returnsf.opensession (); } Public Staticsession Getcurrentsession () {Session session=Threadlocal.get (); if(Session = =NULL) {Session=sf.opensession (); Threadlocal.set (session); } returnsession; } Public Static voidSave (Object obj) {Session session=opensession (); Transaction TX=NULL; Try{TX=session.begintransaction (); Session.save (obj); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) {tx.rollback (); } Throw Newruntimeexception (E.getmessage ()); } finally { if(Session! =NULL&&Session.isopen ()) {Session.close (); }}} @SuppressWarnings ("Unchecked") Public StaticArrayList executeQuery (String hql, string ... params) {Session session=opensession (); Transaction TX=NULL; ArrayList List=NULL; Try{TX=session.begintransaction (); Query Query=session.createquery (HQL); if(Params! =NULL&& params.length > 0) { for(inti = 0; i < params.length; i++) {Query.setparameter (I, params[i]); }} List=(ArrayList) query.list (); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw Newruntimeexception (E.getmessage ()); } finally { if(Session! =NULL&&Session.isopen ()) {Session.close (); } } returnlist; } Public Static voidexecuteupdate (String hql, string ... params) {Session session=opensession (); Transaction TX=NULL; Try{TX=session.begintransaction (); Query Query=session.createquery (HQL); if(Params! =NULL&& params.length > 0) { for(inti = 0; i < params.length; i++) {Query.setparameter (I, params[i]); //System.out.println ("Query influenced:" +params[i]);}} System.out.println ("Query influenced:" +query.getquerystring ()); intn =query.executeupdate (); System.out.println ("Query influence:" +N); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw Newruntimeexception (E.getmessage ()); } finally { if(Session! =NULL&&Session.isopen ()) {Session.close (); } } } }
The reason is that MySQL cannot automatically convert the string type to Integer, and in Oracle you can modify the code as follows:
object[] params = new object[] {20, "Cheng Long"};
Hibernateutil.executeupdate (
"Update Student s set s.sage=?" Where s.sname=? ", params);
PackageCom.huml.util;Importjava.util.ArrayList;Importjava.util.Arrays;Importorg.hibernate.Transaction;ImportOrg.hibernate.Query;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration; Public classHibernateutil {Private Static Finalsessionfactory SF; Private StaticThreadlocal<session> ThreadLocal =NewThreadlocal<session>(); Static{SF=NewConfiguration (). Configure (). Buildsessionfactory (); } PrivateHibernateutil () {} Public StaticSession opensession () {returnsf.opensession (); } Public Staticsession Getcurrentsession () {Session session=Threadlocal.get (); if(Session = =NULL) {Session=sf.opensession (); Threadlocal.set (session); } returnsession; } Public Static voidSave (Object obj) {Session session=opensession (); Transaction TX=NULL; Try{TX=session.begintransaction (); Session.save (obj); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) {tx.rollback (); } Throw Newruntimeexception (E.getmessage ()); } finally { if(Session! =NULL&&Session.isopen ()) {Session.close (); }}} @SuppressWarnings ("Unchecked") Public StaticArrayList executeQuery (String hql, string ... params) {Session session=opensession (); Transaction TX=NULL; ArrayList List=NULL; Try{TX=session.begintransaction (); Query Query=session.createquery (HQL); if(Params! =NULL&& params.length > 0) { for(inti = 0; i < params.length; i++) {Query.setparameter (I, params[i]); }} List=(ArrayList) query.list (); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw Newruntimeexception (E.getmessage ()); } finally { if(Session! =NULL&&Session.isopen ()) {Session.close (); } } returnlist; } Public Static voidexecuteupdate (String hql, Object ... params) {Session session=opensession (); Transaction TX=NULL; Try{TX=session.begintransaction (); Query Query=session.createquery (HQL); if(Params! =NULL&& params.length > 0) { for(inti = 0; i < params.length; i++) {Query.setparameter (I, params[i]); //System.out.println ("Query influenced:" +params[i]);}} System.out.println ("Query influenced:" +query.getquerystring ()); intn =query.executeupdate (); System.out.println ("Query influence:" +N); Tx.commit (); } Catch(Exception e) {if(tx! =NULL) Tx.rollback (); Throw Newruntimeexception (E.getmessage ()); } finally { if(Session! =NULL&&Session.isopen ()) {Session.close (); } } }}
This is the success;