Framework learning-Section 3 of hibernate session interface and get/load/persist Method

Source: Internet
Author: User

1. Session Interface

2. Differences between get and load methods

The Return Value of the load method must not be null. (pay attention to the corresponding an constructor. A default constructor is required)

Lazy loading: if the load method has no search results, the load method constructs an instance of the result class.

// Search for @ override public user finduserbyid (int id) {user u = NULL; Session S = NULL; try {S = hibernateutils. getsession (); U = (User) s. get (user. class, ID); // The default value is to find by ID // lazy loading // U = (User) s. load (user. class, ID); // the return value of the load method can never be blank. Load will not access the database immediately until it is used for the first time. // system. out. println (U. getname () + "" + U. getbirthday (); // if this print statement does not exist, an error is returned! Lazy loading exception} catch (exception e) {e. printstacktrace ();} finally {If (s! = NULL) {S. Close () ;}} return U ;}

 

3. Differences between Sava and persist Methods

When a transaction is not enabled, save can insert data, but the data disappears due to transaction rollback.

The persist method cannot insert data when the transaction is not started. (This is what the video instructor said, but the test result is the opposite !)

I am using hibernate 3.0. The results of the two methods are the same !.... WY...

Use the Save Method

 

Result:

 

No data is inserted into the database.

 

Use the persist Method

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.