Hibernate session. Get () and the Po obtained using SQL statements for update

Source: Internet
Author: User

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.

 

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.