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