Common hibernate database operation code

Source: Internet
Author: User

Insert Method CopyCode The Code is as follows: public void insert (Object O) {session = hibernatesessionfactory. currentsession (); transaction T = session. begintransaction (); Session. save (o); T. commit (); hibernatesessionfactory. closssession ();}

Delete Method Copy codeThe Code is as follows: public void Delete (Object o, serializable ID) {session = hibernatesessionfactory. currentsession (): Transaction T = session. begintransaction (); object o = session. get (O. class, ID); If (o! = NULL) {session. Delete (o);} t. Commit (); hibernatesessionfactory. closssession ();}

Update Method Copy codeThe Code is as follows: public void Update (Object o, serializable ID) {session = hibernatesessionfactory. currentsession (); transaction T = session. begintransaction (); Session. update (O, ID); T. commit (); hibernatesessionfactory. closssession ();}

General select method based on hql Copy codeThe Code is as follows: public arraylist select (string SQL) {session = hibernatesessionfactory. currentsession (); query = createquery (SQL); List list = query. list (); hibernatesessionfactory. closssession (); Return (arraylist) list ;}

SQL-based general select Method Copy codeThe Code is as follows: public arraylist select (string SQL) throws exception {session = hibernatesessionfactory. currentsession (); connection con = session. connection (); preparedstatement pstmt = con. preparedstatement (SQL); resultset rs = pstmt.exe cutequery (); resultsetmetadata rsmd = Rs. getmetadata (); hashtable ht = NULL; arraylist array = new arraylist (); While (RS. next () {ht = new hashtable (); For (INT I = 0; I <rsmd. getcolumncount (); I ++) {ht. put (rsmd. getcolumnname (I + 1), RS. getObject (I + 1);} array. add (HT);} hibernatesessionfactory. closssession (); Return array ;}

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.