SessionOfCrudThe operation is used to query a record. to query multiple or all records, you need to useHibernateProvidedHqlLanguage.
Crud operations on sessions in hibernate:
What is the difference between get and load?
* Get does not support lazy, and load supports lazy.
* Get is used to load data. If no matching data exists, null is returned, while load throws an exception.
What are the features of the transient status?
* No matching data exists in the database.
* Not included in session management
Persistent status features?
* Objects in the persistent State have matched data in the database.
* Added session management.
* When the cache is cleared (dirty data check), it will be synchronized with the database
What are the features of the detached status?
* Matched data exists in the database.
* Not included in session management
Import java. util. date;
Import org. hibernate. Session;
Import org. hibernate. transaction;
Import JUnit. Framework. testcase;
Public class sessiontest extends testcase {
Public void testhello1 (){
System. Out. println ("----------- sessiontest. hello1 ()------------");
// Throw new java. Lang. runtimeexception ();
}
Public void testhello2 (){
System. Out. println ("----------- sessiontest. testhello2 ()------------");
// This. assertequals ("hello", "hello111 ");
}
Public void testsave1 (){
Session session = NULL;
Transaction Tx = NULL;
User user = NULL;
Try {
Session = hibernateutils. getsession ();
Tx = session. begintransaction ();
// Transient status
User = new user ();
User. setname ("Li Si ");
User. setpassword ("123 ");
User. setcreatetime (new date ());
User. setexpiretime (new date ());
// Persistent status. When the attribute changes, Hibernate automatically synchronizes with the database.
Session. Save (User );
User. setname ("Wang Wu ");
// Session. Update (User );
TX. Commit ();
} Catch (exception e ){
E. printstacktrace ();
TX. rollback ();
} Finally {
Hibernateutils. closesession (session );
}
// Detached status
User. setname ("Zhang San ");
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// Persistent status
Session. Update (User );
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testreadbygetmethod1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// Issue the query SQL statement immediately to load the user object
User user = (User) Session. Get (user. class, "402880d01b9bf210011b9bf2a2ff0001 ");
System. Out. println ("user. Name =" + User. getname ());
// Persistent status. When the attribute changes, Hibernate automatically synchronizes with the database.
User. setname ("Long ge ");
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testreadbygetmethod2 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// Use get to load data. If no data exists in the Database, null is returned.
User user = (User) Session. Get (user. class, "asdfsafsdfdsf ");
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testreadbyloadmethod1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// The query SQL statement is not sent because the load method implements lazy (lazy loading or delayed loading)
// Delayed loading: This object is loaded only when it is actually used (issuing an SQL statement)
// The implementation principle of hibernate delayed loading is proxy.
User user = (User) Session. Load (user. class, "402880d01b9bf210011b9bf2a2ff0001 ");
System. Out. println ("user. Name =" + User. getname ());
// Persistent status. When the attribute changes, Hibernate automatically synchronizes with the database.
User. setname ("fat brother ");
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testreadbyloadmethod2 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// Load the data. If the database does not have the corresponding data
// Throw objectnotfoundexception
User user = (User) Session. Load (user. class, "55555555 ");
System. Out. println (user. getname ());
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
Throw new java. Lang. runtimeexception ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testupdate1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// Manually constructed detached state object
User user = new user ();
User. setid ("402880d01b9be8dc011b9be9b23d0001 ");
User. setname (" ");
Session. Update (User );
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testdelete1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
//// Manually constructed detached state object
// User = new user ();
// User. setid ("402880d01b9be8dc011b9be9b23d0001 ");
// User. setname ("Dehua ");
// Session. Delete (User );
User user = (User) Session. Load (user. class, "402880d01b9be8dc011b9be9b23d0001 ");
Session. Delete (User );
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
// Transient status
}
}