Hibernate Session Get Connection

Source: Internet
Author: User
Tags deprecated rowcount

Hibernate Session Get Connection

As a result of a recent project to use an auxiliary SQL, hibernate inside the sqlquery API total SQL statement can not contain: colon, solid discard hibernate, direct use of JDBC.

Hibernate3.3.2 version getsession (). Connection () deprecated, alternative method

Sessionfactoryutils.getdatasource (Getsessionfactory ()). getconnection ()

Original method

GetSession (). Connection ()

From class Org.springframework.orm.hibernate3.SessionFactoryUtils

Example:

Java.sql.Connection C =NULL; Java.sql.PreparedStatement PS=NULL; Java.sql.ResultSet RS=NULL; Publiclist method (String sql) {list ret=NewArrayList (); Try{C=Sessionfactoryutils.getdatasource (Getsessionfactory ()). getconnection (); PS=c.preparestatement (SQL); RS=Ps.executequery ();  while(Rs.next ()) {...}            Ret.add (RO); }        } Catch(Exception e) {e.printstacktrace (); } finally{close (); }        returnret; }

My demo

/**     *      *      * @authorMjorcen * @email [email protected].com * @dateTime Jul 4, 5:43:05 PM *@version1 *@throwsSQLException*/    Private voidGetTop ()throwsSQLException {StringBuilder sb=NewStringBuilder (); Sb.append ("Select x.comment_id, X.total_no, X.order_no, Ifnull (X.order_no/x.total_no, 1) as Percentage_no"); Sb.append ("From (select @rowNum: = @rowNum + 1 as order_no, @rowCount as Total_no, t.comment_id, t.comment_spending"); Sb.append ("From T_sd_comment T, (select @rowNum: = 0"); Sb.append (") b, (select @rowCount: = ("); Sb.append ("Select COUNT (*)"); Sb.append ("From T_sd_comment"); Sb.append ("WHERE to_days (create_time) >= to_days (now ())"); Sb.append ("                )  "); Sb.append (") C"); Sb.append ("WHERE to_days (t.create_time) >= to_days (now ()) ORDER by t.comment_spending, t.comment_id"); Sb.append (") X"); Sb.append ("WHERE x.comment_id =?" "); Connection C= This. Commentservice.getsession (). connection (); PreparedStatement PS=c.preparestatement (sb.tostring ()); Ps.setint (1, This. Paramcomment.getcommentid ()); ResultSet RS=Ps.executequery (); if(Rs.next ()) {System.out.println ("comment_id =" + Rs.getint ("comment_id")); System.out.println ("Total_no =" + Rs.getint ("Total_no")); System.out.println ("Order_no =" + Rs.getint ("Order_no")); System.out.println ("Percentage_no =" + rs.getdouble ("Percentage_no")); }    }

The Hibernate API lets you use DoWork (Work,work), which is described as follows:

Connection ()
Deprecated. (scheduled for removal in 4.x). replacement depends on need; For doing direct JDBC stuff use doWork(org.hibernate.jdbc.Work) , for opening a ' temporary Session ' use (TBD).

The Execute () method of the work interface is used to perform operations that access the database directly through the JDBC API:

 Public Interface Work {    //  access the database directly through the JDBC API public    voidthrows SQLException;}

The DoWork method of the session is used to perform the operation specified by the work object, called the Execute () method of the Working object.

The session will pass the currently used database connection to the Execute () method.

The process is as follows:

Transaction tx = session.begintransaction ();     // defines an anonymous class that implements the work interface    New Work () {        publicvoidthrows  SQLException {            // executing SQL statements for bulk updates through the JDBC API            PreparedStatement stmt = connection                    . Preparestatement ("Update CUSTOMERS set age=age+1"                            + "where Age>0 ");            Stmt.executeupdate ();        }    };

Perform work

Session.dowork (work); Tx.commit ();

When SQL statements are executed through the PreparedStatement interface in the JDBC API, the data involved in the SQL statement is not loaded into the session's cache and therefore does not consume memory space.

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.