Introduction to hibernate common methods

  • 2020-04-01 02:20:17
  • OfStack

I. common methods of hibetatetemplate

1. Delete (Object entity) deletes the specified persistent instance
It is usually used first in programs       Assert. NotNull and Assert. IsTrue Assert whether entity is null and whether the id of entity is greater than 0
Otherwise the transaction rolls back. The persistence is then queried using get(Class entityClass,Serializable id,LockMode LockMode) with a lock
To set an instance, we usually use lockmode.update pessimistic lock, and then delete(Object entity) to delete the instance.

2. DeleteAll (Collection entities) deletes all persistent instances in the Collection
Entities must be a persistent instance, otherwise a database exception is reported.

Find (String queryString) returns the collection of instances based on the HQL queryString
The find method first looks for the cache when it is executed, then looks for the database if the cache is not found, and returns null if it is not found.

4. Get (Class entityClass,Serializable id) loads a specific persistent instance according to the primary key
It is usually used first in programs         Assert. IsTrue asserts whether id is greater than 0, continues execution if greater than 0, returns an instance if data is found, otherwise returns null
Unlike load, load returns an instance if it has data, otherwise it reports an ObjectNotFoundEcception, which is more efficient to get

5. Save (Object entity) the new instance
It is usually used first in programs       Assert. NotNull asserts that the entity is empty and is being saved.

6. SaveOrUpdate (Object entity) select saveOrUpdate according to the state of the instance
This method contains the functions of both save and update methods. If the parameter passed in is temporary object vo without id, the save method will be called. If pass
If the parameter entered is that the free object has an id, the update() method is called. If the parameter passed in is the persistent object Po, it is returned directly.

Update (Object entity) the state instance of the update instance must be persistent
It is usually used first in programs       Assert. NotNull and Assert. IsTrue Assert whether entity is null and whether the id of entity is greater than 0,
Then in the update instance, the entity must have an id or it cannot be updated.

8. Object execute (hibetnateCallback action) is the HibernateCallback interface class.
Consider this approach when the methods in the dao do not meet the program's needs. For example, if a unique entity needs to be returned according to several different parameters, use uniqueResult().
HibernateCallback in defines a doInHibernate method, this method is variable. If you want to find data the method should be
Session.load (). Session.delete (). In this case, the method to query the database is the execute method.

 
9. BulkUpdate () directly adds, updates and deletes entities through statements
Generally, there are two types of bulkUpdate(String sqlString) and bulkUpdate(String sqlString,Object[] Object)
Method that returns the number of increments, updates, or deletes commonly used in batches.
 
10. Hibernate primary key generation strategy
 
(1) Assigned: when inserting data, the primary key is added by the user. Such as: < The generator class = "assigned" / >
(2) sequence: the sequence of the database is called to generate the primary key. The sequence name should be set, otherwise hibernate cannot find it.
Such as: < Param    Name = "sequence" > ENTTY_NAME_SEQ < / param>
(3) hibernate will add a self-increasing primary key to the primary key when increment inserts data, but a hibernate instance will maintain a counter.
So you can't use this method when multiple instances are running.

11. Three states of entity objects
The lifetime of a Session is bound to a physical transaction (tansaction).
The main function of Session is to provide the creation, reading, and deletion of mapped entity class instances
(1) free state (transient) : it has not been persisted and is not associated with any Session
(2) persistent state: only associated with a Session
A persistent instance can become free by calling delete(). Instances obtained through the get() or load() methods are persisted
(3) the free state (detached) : has been persisted, but the current was not associated with any Session
Free state of the instance by calling the update (), the lock (), replicate (), save (), persist () or saveOrUpdate () method for persistence

Related articles: