List Hibernate Session interface methods.
Unlike SessionFactory, the Session object will be created on demand. Session is a lightweight object. Session
provides a physical connectivity between your application and database. The Session will be established each time your application
wants do something with database. Session object will be provided by SessionFactory object. All the persistent objects will be
saved and retrieved through Session object. The session object must be destroyed after using it.
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. The main function of
the Session is to offer create, read and delete operations for instances of mapped entity classes.
Here is the list of methods which will be used very often in Hibernate implementations:
beginTransaction()
Begin a unit of work and return the associated Transaction object. This method needs
to be called if you want to enable transaction, and once your DB interactions are done, call commit() method on the returned
transaction object. Incase of any issues, call rollback() error on the transaction object.
save()
Persist the given transient instance, first assigning a generated identifier. This method stores the given object in the
database. Before storing, it checks for generated identifier declaration and process it first, then it will store into DB.
update()
Update the persistent instance with the identifier of the given detached instance. It updates the database record.
saveOrUpdate()
Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks.
This operation cascades to associated instances if the association is mapped with cascade="save-update".
createQuery()
Create a new instance of Query for the given HQL query string.
createSQLQuery()
Create a new instance of SQLQuery for the given SQL query string.
merge()
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent
instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is
unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the
session. This operation cascades to associated instances if the association is mapped with cascade="merge".
persist()
Make a transient instance persistent. This operation cascades to associated instances if the association is mapped
with cascade="persist".
flush()
Force this session to flush. Must be called at the end of a unit of work, before committing the transaction and closing the session.
Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.
delete()
Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving
Session or a transient instance with an identifier associated with existing persistent state. This operation cascades to
associated instances if the association is mapped with cascade="delete".
|