1. Hibernate often uses configuration
Using the Hibernate.default_schema property enables all generated tables to have a specified prefix.
2. Session Brief Introduction
It is not recommended to use the JDBC connection operation database directly, but to manipulate the database by using the session.
Session can be understood as the object that operates the database. The session and the connection are many-to-one relationships. Each session has a corresponding connection. A connection can be used for multiple sessions at different times. Save (), update (), delete (), CreateQuery (), and other methods that are required to invoke the session in the relational database.
3. Transaction Brief Introduction
Hibernate operations on the data are encapsulated within the transaction, and the default is not the way to commit voluntarily. So when you save the object with the session. Suppose the transaction is not opened and the transaction is committed manually. Objects are not actually saved in the database.
If you want hibernate to commit a transaction on its own, like JDBC, you must invoke the DoWork () method of the Session object. After obtaining the connection of JDBC. Set it to commit the transaction mode for itself.
Like what:
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 Specific explanation
There are two ways to get the Session object:
(1). Sessionfactory's Opensession () method
(2) Sessionfactory's Getcurrentsession () method
Assume that the use of getcurrentsession requires configuration in the Hibernate.cfg.xml file:
Assumed to be a local transaction (JDBC Transaction)
<property name= "Hibernate.current_session_context_class" >thread</property>
Assumed to be a global transaction (JTA Transaction)
<property name= "Hibernate.current_session_context_class" >jta</property>
the difference between opensession and getcurrentsession:
(1). Getcurrentsession shuts itself down after a transaction commits or rolls back, and opensession needs to be shut down manually. Assume that you are using opensession instead of manually closing. After multiple times, the connection pool overflows.
(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 Service Register 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 Register 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 ();}
through the example we can see that without closing the session, the use of opensession each time to obtain a new session, and the use of Getcurrentsession is the same session, So after the session operation obtained using Opensession is complete. Manual close Off is required.
5. HBM configuration files often use settings
<class> tags often use attributes:
Frequently used properties for <id> tags:
Common primary key generation policies:
"Hibernate" (2) Hibernate configuration with session, transaction