"Hibernate" (2) Hibernate advanced

Source: Internet
Author: User

1. Hibernate Common Configuration


Use the Hibernate.default_schema property to have all generated tables with a specified prefix.


2. Session Introduction

It is not recommended to use the JDBC connection operation database directly, but to manipulate the database by using the session. The session can be understood as an object that operates the database. Session and connection are many-to-one relationships, each session has a corresponding connection, a connection at different times can be used for multiple sessions. Save (), update (), delete (), CreateQuery (), and many other methods that need to call the session in the relational database.


3. Transaction Introduction

Hibernate operations on data are encapsulated in transactions, and the default is non-autocommit. So when you save an object with a session, if you do not open the transaction and commit the transaction manually, the object is not actually saved in the database.

If you want hibernate to commit the transaction automatically like JDBC, you must call the DoWork () method of the Session object, and after getting the connection of JDBC, set it to autocommit transaction mode.

For example:

Session.dowork (new work () {@Overridepublic void execute (Connection conn) throws SQLException {Conn.setautocommit (True) ;}}); Session.save (S1); Save object into Database Session.flush ();
Note that you must not forget the flush () method.

4. Session explanation

There are two ways to get the Session object:

(1). Sessionfactory's Opensession () method

(2) Sessionfactory's Getcurrentsession () method

If you are using getcurrentsession, you need to configure it in the Hibernate.cfg.xml file:

If it is a local transaction (JDBC Transaction)

<property name= "Hibernate.current_session_context_class" >thread</property>

If it is a global transaction (JTA Transaction)

<property name= "Hibernate.current_session_context_class" >jta</property>
the difference between opensession and getcurrentsession:

(1). Getcurrentsession automatically shuts down after a transaction commits or rolls back, and opensession requires you to close it manually. If you use opensession without a manual shutdown, multiple times will cause the connection pool to overflow.

(2). Opensession each time a new session object is created, and Getcurrentsession uses the existing session object.

@Testpublic void Testsavestudentwithopensession () {//Get Config Object configuration config = new configuration (). Configure ();// Get the Service registration object Serviceregistry Serviceregistry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry ();//Get Sessionfactory object Sessionfactory sessionfactory = Config.buildsessionfactory ( Serviceregistry);//Get Session Object Session Session1 = Sessionfactory.opensession ();//open things transaction Transaction =  Session1.begintransaction ();//Generate a Student object Student s = new Student (1, "John Doe", "male", New Date (), "Beijing"); Session1.dowork (new Work () {@Overridepublic void execute (Connection conn) throws SQLException {System.out.println ("Connection hashcode:" + Conn.hashcode ());}); Session1.save (s);//Session1.close ();//COMMIT Transaction Transaction.commit ();//Create a session to submit session Session2 = Sessionfactory.opensession (); transaction = Session2.begintransaction (); s = new Student (2, "Harry", "Male", New Date (), "Shanghai"); Session2.dowork (new work () {@Overridepublic void execute (Connection conn) throws SQlexception {System.out.println ("Connection hashcode:" + conn.hashcode ());}); Session2.save (s); Transaction.commit ();} @Testpublic void Testsavestudentwithgetcurrentsession () {//Get Config Object configuration config = new configuration (). Configure ();//Get Service Registration object Serviceregistry Serviceregistry = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry ();//Get Sessionfactory object Sessionfactory sessionfactory = Config.buildsessionfactory ( Serviceregistry);//Get Session Object Session Session1 = Sessionfactory.getcurrentsession ();//open things transaction Transaction =  Session1.begintransaction ();//Generate a Student object Student s = new Student (1, "John Doe", "male", New Date (), "Beijing"); Session1.dowork (new Work () {@Overridepublic void execute (Connection conn) throws SQLException {System.out.println ("Connection hashcode:" + Conn.hashcode ());}); Session1.save (s);//Session1.close ();//COMMIT Transaction Transaction.commit ();//Create a session to submit session Session2 = Sessionfactory.getcurrentsession (); transaction = Session2.begintransaction(); s = new Student (2, "Harry", "Male", New Date (), "Shanghai"), Session2.dowork (new work () {@Overridepublic void execute (Connection con N) throws SQLException {System.out.println ("Connection hashcode:" + conn.hashcode ());}); Session2.save (s); Transaction.commit ();}
we can see from the example that, without closing the session, every time you get a new session using Opensession, you get the same session using Getcurrentsession, Therefore, you need to close the session manually after you have finished using Opensession.


5. HBM configuration file Common settings


<class> Tags Common Properties:


Common Properties for <id> tags:


Common primary key generation policies:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Hibernate" (2) Hibernate advanced

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.