Hibernate in Update HQL statement

Source: Internet
Author: User
Tags getmessage

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;

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.