Saturday, June 22, 2019

Hibernate Session Interface Methods

List Hibernate Session interface methods.

evict : Remove this instance from the session cache.
void evict(Object object) throws HibernateException
void clear(): Completely clear the session. Evict all loaded instances and cancel all pending saves, updates and deletions. Do not close open iterators or instances of ScrollableResults.

Object load(Class theClass, Serializable id, LockMode lockMode) throws HibernateException
Object get(Class clazz, Serializable id) throws HibernateException

void replicate(Object object, ReplicationMode replicationMode) throws HibernateException
Serializable save(Object object) throws HibernateException
void saveOrUpdate(Object object) throws HibernateException
void update(Object object) throws HibernateException
Object merge(Object object) throws HibernateException
void persist(Object object) throws HibernateException
  • Return type of persist methods is void. This will be used only within transaction boundaries. 
  •  save(), update() & saveOrUpdate() can use without transaction also, but mapped objects not getting saved if session is not flushed.
  • The persist() &  merge() operation can only be called within a transaction, an exception will be thrown outside of a transaction.
  • It is useful in long-running conversations with an extended Session context.
  • persist() method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.
  • Any further changes are tracked. If the object properties are changed before the transaction is committed or session is flushed, it will also be saved into database.
Note: persist() is supported by JPA, while save() is only supported by Hibernate.
void delete(Object object) throws HibernateException
void lock(Object object, LockMode lockMode) throws HibernateException
Session.LockRequest buildLockRequest(LockOptions lockOptions)
void refresh(Object object) throws HibernateException
getCurrentLockMode: Determine the current lock mode of the given object.
LockMode getCurrentLockMode(Object object) throws HibernateException
createFilter: Create a new instance of Query for the given collection and filter string.
Query createFilter(Object collection, String queryString) throws HibernateException
getStatistics: Get the statistics for this session.
SessionStatistics getStatistics()
contains: Check if this instance is associated with this Session.
boolean contains(Object object)

Serializable getIdentifier(Object object) throws HibernateException
isDirty: Does this session contain any changes which must be synchronized with the database?
boolean isDirty() throws HibernateException
flush: Force this session to flush.
void flush() throws HibernateException

flush is used to save an entity immediately into the database. So it can be called multiple times in a transaction to save the entity piece-by-piece i.e. it's synchronize database with the current state of object/objects held in the memory but it does not commit the transaction.
While getTransaction().commit() also flushes the data to the database but it also marks the end of the current transaction.
So, if you get any exception after flush() is called, then the transaction will be rolled back. commit() will make data stored in the database permanently.


Related Tutorials

No comments:

Post a Comment