Hibernate code to execute HQL statements directly

Source: Internet
Author: User

Execute HQL statement directly with Hibernate, not manipulate object directly

Example one:

public int ExecuteSQL (String sql, map<string, object> params) {sqlquery sqlquery = getsession (). Createsqlquery (SQL ); if (params!= null) {sqlquery.setproperties (params);//Pass Value} return Sqlquery.executeupdate (); }

Example two:

public void Updateparameter (string Configureid, String configurevalue) {string hql = ' UPDATE ffconfigure SET configureval Ue=:configurevalue WHERE Configureid=:configureid "; SQLQuery sqlquery = GetSession (). Createsqlquery (HQL); map<string,object> map = new hashmap<string,object> (); Map.put ("Configurevalue", Configurevalue); Map.put ("Configureid", Configureid); Sqlquery.setproperties (map); Sqlquery.executeupdate (); }

Example three:

public void Updateparameter (final string configureid, final string configurevalue) throws Exception {Gethibernatetemplat E (). Execute (new Hibernatecallback () {public Object doinhibernate throws hibernateexception {String hql = "UPDATE ffconfigure SET configurevalue=?" WHERE configureid=? "; Query q = session.createquery (HQL); Q.setstring (0, Configureid); Q.setstring (1, configurevalue); Q.executeupdate (); return null; } }); }

Example one, and the example of the beginning is the same, are used sqlquery, very good.

Example three uses a hibernate callback, spring with the transaction situation, update when the background will send SQL statements, but the database is not updated, do not know why.

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.