The SAVE () method provides the function of adding data to the database, but can only be added. This Dao does not generate the update () method.
However, you can simply rename the eight save () method with the update function: Change getsession (). Save * (transientinstance );
Getsession (). Merge (transientinstance); or getsession (). saveorupdate
(Transientinstance );
Public void save (User transientinstance ){
Log. debug ("Saving user instance ");
Try {
Session session = getsession ();
Transaction Tx = session. begintransaction ();
Session. Save (transientinstance );
TX. Commit ();
Session. Close ();
Log. debug ("Save successful ");
} Catch (runtimeexception re ){
Log. Error ("save failed", RE );
Throw re;
}
}
The Delete () method is used to delete objects. In fact, we will use the following method to delete objects.
Public void Delete (integer ID ){
Log. debug ("deleting user instance... ");
User user = findbyid (ID );
Delete (User );
}
Public void Delete (User persistentinstance ){
Log. debug ("deleting user instance ");
Try {
Session session = getsession ();
Transaction Tx = session. begintransaction ();
Session. Delete (persistentinstance );
TX. Commit ();
Session. Close ();
Log. debug ("delete successful ");
} Catch (runtimeexception re ){
Log. Error ("delete failed", RE );
Throw re;
}
}
Search by number
Public user findbyid (Java. Lang. Integer ID ){
Log. debug ("getting user instance with ID:" + id );
Try {
User instance = (User) getsession (). Get ("HBM. User", ID );
Return instance;
} Catch (runtimeexception re ){
Log. Error ("Get failed", RE );
Throw re;
}
}
The findbyexample () method provides the function of "select * From usertable", which is used to query all data.
Public list findbyexample (user instance ){
Log. debug ("finding user instance by example ");
Try {
List Results = getsession (). createcriteria ("HBM. User"). Add (
Example. Create (Instance). List ();
Log. debug ("find by example successful, result size :"
+ Results. Size ());
Return results;
} Catch (runtimeexception re ){
Log. Error ("find by example failed", RE );
Throw re;
}
}
The findbyproperty () method is used to flexibly provide a conditional query method. You can define the query method by yourself.
Public list findbyproperty (string propertyname, object Value ){
Log. debug ("finding user instance with property:" + propertyname
+ ", Value:" + value );
Try {
String querystring = "from user as model where model ."
+ Propertyname + "=? ";
Query queryobject = getsession (). createquery (querystring );
Queryobject. setparameter (0, value );
Return queryobject. List ();
} Catch (runtimeexception re ){
Log. Error ("find by property name failed", RE );
Throw re;
}
}
Public list findbyname (Object Name ){
Return findbyproperty (name, name );
}
Public list findbysex (Object sex ){
Return findbyproperty (sex, sex );
}
Public list findbyage (Object age ){
Return findbyproperty (age, age );
}
Public list findall (){
Log. debug ("finding all user instances ");
Try {
String querystring = "from user ";
Query queryobject = getsession (). createquery (querystring );
Return queryobject. List ();
} Catch (runtimeexception re ){
Log. Error ("Find all failed", RE );
Throw re;
}
}
Copy the attributes of the passed object in the detached state to the persistent object, and return the persistence object. If there is no associated Persistent object in the session, load one. If the passed object is not saved, save a copy and return it as a persistent object. The passed object remains in the detached state.
Can be used to update data
Public user merge (User detachedinstance ){
Log. debug ("merging user instance ");
Try {
Session session = getsession ();
Transaction Tx = session. begintransaction ();
User result = (User) Session. Merge (detachedinstance );
TX. Commit ();
Session. Close ();
Log. debug ("merge successful ");
Return result;
} Catch (runtimeexception re ){
Log. Error ("merge failed", RE );
Throw re;
}
}
Persists and saves the passed object. If the object is not saved (transient status), call the Save method to save the object. If the object is saved (in the detached state), call the update method to re-associate the object with the session.
Public void attachdirty (user instance ){
Log. debug ("attaching dirty user instance ");
Try {
Getsession (). saveorupdate (instance );
Log. debug ("Attach successful ");
} Catch (runtimeexception re ){
Log. Error ("Attach failed", RE );
Throw re;
}
}
Sets the object status to transient.
Public void attachclean (user instance ){
Log. debug ("attaching clean user instance ");
Try {
Getsession (). Lock (instance, lockmode. None );
Log. debug ("Attach successful ");
} Catch (runtimeexception re ){
Log. Error ("Attach failed", RE );
Throw re;
}
}