1. You can overload the method to specify the configuration file
New Annotationconfiguration (). Configure ("hibernate.xml"). Buildsessionfactory ();
So the reading is hibernate.xml but generally not recommended to modify
Getcurrentsession refers to a context that does not create a new session if it is not committed
Opensession always opens a new session for authenticating transaction boundaries such as join log operations, etc.
Transaction: Either done at the same time or rolled back
<property name="current_session_context_class">thread</property>
Find from current thread
There are several other parameters:
such as JTA, Managed,custorm.class, etc.
Thread uses database links as transaction management
But you can't manage distributed transactions
JTA (Java trasaction API) is often used in distributed
Requires a manager to manage operations on a two-database deployment
If you don't set the context, he can't find it.
Opensession try not to use the same session with Getcurrentsession.
A cache is an area of memory where we want to improve the efficiency of reading objects
Three states of an object
Transient Persistent Detached
Transient memory has an object but there is no database in the ID cache and no
There is a database in the persistent cache and there is an ID session with key and value
There is no database in detached cache
Three states are distinguished by:
There is no ID
ID in the database has no
Not in memory (session cache)
Session Management Task Unit management of a database operation of the increment and revise check
Save slightly
Delete
As long as the ID includes a detach state, you can delete
For example:
@Test Public voidTestdelete () {Teacher T=NewTeacher (); T.setname ("T1"); T.settitle ("Middle"); T.setbirthdate (NewDate ()); Session Session=sessionfactory.getcurrentsession (); Session.begintransaction (); Session.save (t); System. out. println (T.getid ()); Session.gettransaction (). commit (); Session Session2=sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.delete (t); Session2.gettransaction (). commit (); } @Test Public voidTestDelete2 () {Teacher T=NewTeacher (); T.setid (2); Session Session2=sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.delete (t); Session2.gettransaction (). commit (); }
Load Read
@Test Public void testLoad () { = sessionfactory.getcurrentsession (); Session.begintransaction (); = (Teacher) session.load (Teacher. Class1); Session.gettransaction (). commit (); System. out . println (T.getclass ()); // System.out.println (T.getname ()); }
1 can be automatically packaged
Get can also take data
@Test publicvoid testget () { = Sessionfactory.getcurrentsession (); Session.begintransaction (); = (Teacher) session. Get (Teacher. class 1 ); Session.gettransaction (). commit (); System. out . println (T.getclass ()); // System.out.println (T.getname ()); }
The difference between the two:
It is important to note that the get and load methods have significant differences. The load type is a proxy that generates an SQL statement when you really want to use the properties of the object and get is immediately sent
Update
Update an object of detach
@Test public void TestUpdate1 () {Session session = sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher t = (Teacher) session. get (Teacher. class , 1 ); Session.gettransaction (). commit (); T.setname ( " zhanglaoshi " ); Session session2 = sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.update (t); Session2.gettransaction (). commit (); }
Update transient will error but update with ID transient not error (database has corresponding record)
@Test publicvoid testUpdate4 () { = Sessionfactory.getcurrentsession (); Session.begintransaction (); = (Teacher) session. Get (Teacher. class 1 ); T.setname ("zhangsan2"); Session.gettransaction (). commit (); }
The above update will update all fields to improve efficiency so grope
How to change only the fields you want
If it is a P object, the update will occur whenever you set the field
@Test Public voidTestUpdate5 () {Session session=sessionfactory.getcurrentsession (); Session.begintransaction (); Student s= (Student) session.Get(Student.class,1); S.setname ("Zhangsan5"); Session.gettransaction (). commit (); //discover objects with different automatic update in databaseS.setname ("Z4"); Session Session2=sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.update (s); Session2.gettransaction (). commit (); }
But this update is still the whole
To implement updating only the fields you want to update
There are three ways to do this:
1. Add Column = False property
or XML joins column = False property
2. Add in XML
<class name="com.bjsxt.hibernate.Student"dynamic-update=" true">
This will only update the fields you want to change when the database is submitted.
However, it is important to note that this object cannot be updated so that it is updated or all fields once it is in the detach state and commits a commit.
@Test Public voidTestUpdate6 () {Session session=sessionfactory.getcurrentsession (); Session.begintransaction (); Student s= (Student) session.Get(Student.class,1); S.setname ("zhangsan6"); Session.gettransaction (). commit (); S.setname ("Z4"); Session Session2=sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.merge (s);//merge can merge objects so that there is no need to send extra modifications to other fields session2.gettransaction (). commit (); }
The third is that the HQL statement will also be detailed later
@Test Public void TestUpdate7 () { = sessionfactory.getcurrentsession (); Session.begintransaction (); = Session.createquery ("update Student s set s.name= ' Z5 ' where s.id = 1"); Q.executeupdate (); Session.gettransaction (). commit (); }
Save Orupdate
@Test Public voidtestsaveorupdate () {Teacher T=NewTeacher (); T.setname ("T1"); T.settitle ("Middle"); T.setbirthdate (NewDate ()); Session Session=sessionfactory.getcurrentsession (); Session.begintransaction (); Session.saveorupdate (t); Session.gettransaction (). commit (); T.setname ("T2"); Session Session2=sessionfactory.getcurrentsession (); Session2.begintransaction (); Session2.saveorupdate (t); Session2.gettransaction (). commit (); }
If you don't have anything, save it and update it.
Clear
Either load or get will first look for the cache (one-level cache), if not, will go to the database lookup, call the Clear method to force clear session cache
@Test Public voidTestclear () {Session session=sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher T= (Teacher) session.load (Teacher.class,1); System. out. println (T.getname ()); Session.clear ();//If you do not add this sentence will only issue a select SQL statement, added after clearing the session cache below the need to re-send the Teacher T2= (Teacher) session.load (Teacher.class,1); System. out. println (T2.getname ()); Session.gettransaction (). commit (); }
Flush method
Forcing cached content to synchronize with database content
Specifically at what time the flush is set by Flushmode
@Test Public voidTestflush () {Session session=sessionfactory.getcurrentsession (); Session.begintransaction (); Teacher T= (Teacher) session.load (Teacher.class,1); T.setname ("TTTT"); Session.flush (); Forces synchronization with the database, issuing an update t.setname ("TTTTT"); Session.gettransaction (). commit (); Find inconsistencies and then issue update}
Testschemaexport
You can not let him build the table automatically in the configuration file.
Then write your own class-building table
First parameter this refers to whether the DDL statement is displayed the second parameter is whether to execute the DDL statement
@Test Public void Testschemaexport () { new schemaexport (new annotationconfiguration (). Configure ()). Create (falsetrue); }
Hibernate re-engagement with core development interfaces