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
clear 
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.

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

replicate
void replicate(Object object, ReplicationMode replicationMode) throws HibernateException
save
Serializable save(Object object) throws HibernateException
saveOrUpdate
void saveOrUpdate(Object object) throws HibernateException
update
void update(Object object) throws HibernateException
merge
Object merge(Object object) throws HibernateException
persist
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.
delete
void delete(Object object) throws HibernateException
lock
void lock(Object object, LockMode lockMode) throws HibernateException
buildLockRequest
Session.LockRequest buildLockRequest(LockOptions lockOptions)
refresh
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)
getIdentifier

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.

Reference: https://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html


Related Tutorials

No comments:

Post a Comment