Hibernate: Use session in Dao. the PO obtained from get () or SQL statements does not need session when you set the value. update can also be saved. In order to prove that the Po acquisition set in Dao will be saved, I did the following experiment:
1. Fetch and store data in Dao
Public void sessionget (){
Test po = (TEST) Session. Get (test. class, "1 ");
Po. Seta ("111aaa ");
}
Public void sqlget (){
String SQL = "from test t where T. ID =: ID ";
Query q = session. createquery (SQL );
Q. setstring ("ID", "2 ");
Test po = (TEST) Q. uniqueresult ();
Po. Seta ("222aaa ");
String sql1 = "from test t where T. ID =: ID ";
Query Q1 = session. createquery (sql1 );
Q1.setstring ("ID", "6 ");
List lst = q1.list ();
If (LST! = NULL ){
Test PO1 = (TEST) lst. Get (0 );
Po1.seta ("666aaa ");
}
}
All the above methods are saved.
I tried again. Put the Po Method in one method, call another method, and set the method as follows:
Public void methodget (){
Test po = This. getsqltest ();
Po. Seta ("4aaa ");
Test PO1 = This. getsessiontest ();
Po1.seta ("5aaa ");
}
Test getsqltest (){
String SQL = "from test t where T. ID =: ID ";
Query q = session. createquery (SQL );
Q. setstring ("ID", "4 ");
Test po = (TEST) Q. uniqueresult ();
Return po;
}
Test getsessiontest (){
Test po = (TEST) Session. Get (test. class, "5 ");
Return po;
}
The result is saved, and the result shows that the data is saved as long as the Po is taken out at the DaO layer and set data is performed without update.
2. retrieve data from Dao at the service layer for set, and retrieve data from Dao at the action layer for set,
So I tried to extract the Po from the DaO layer and put it to the service layer to set, without update. At this time, the data can still be saved.
However, I tried to extract the Po from the DaO layer and put it to the action layer to set it. Without update, the data could not be saved at this time.
This proves that data can be stored without updating when the set value is set in the same transaction. However, data cannot be saved when set is performed in a non-transaction.
3. retrieving data from Dao goes through the action layer and then enters the service layer for set. At this time, no data can be saved.
Summary: As long as set data is performed on PO in the same transaction, whether it is Dao or service layer, and no update is required, the data will be saved.
Instead of the same transaction, data cannot be saved without update.