You are on page 1of 2

Session openSession() : Open a Session. Session openSession(Connection connection): Open a Session, utilizing the specfied JDBC Connection.

boolean isClosed():--Is this factory already closed? Session getCurrentSession(): Obtains the current session. void close() :- Destroy this SessionFactory and release all resources (caches, connection pools, etc). Serializable save(Object object): Persist the given transient instance, first assigning a generated identifier. void update(Object object) void refresh(Object object) : Re-read the state of the given instance from the underlying database. void persist(Object object): Make a transient instance persistent. void load(Object object, Serializable id): Read the persistent state associated with the given identifier into the given transient instance. boolean isOpen() Check if the session is still open. boolean isConnected() Check if the session is currently connected. void flush() Force this session to flush. void evict(Object object) Remove this instance from the session cache. Connection disconnect() Disconnect the Session from the current JDBC connection. void delete(Object object) Remove a persistent instance from the datastore. SQLQuery createSQLQuery(String queryString) Create a new instance of SQLQuery for the given SQL query string. Query createQuery(String queryString):Create a new instance of Query for the given HQL query string. Criteria createCriteria(String entityName, String alias) Create a new Criteria instance, for the given entity name, with the given alias. Criteria createCriteria(String entityName) Create a new Criteria instance, for the given entity name. Connection Transaction object. close() End the session by releasing the JDBC connection and cleaning up. beginTransaction() Begin a unit of work and return the associated Transaction

The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states: transient: never persistent, not associated with any Session persistent: associated with a unique Session detached: previously persistent, not associated with any Session Transient instances may be made persistent by calling save(), persist() or saveOrUpdate(). Persistent instances may be made transient by calling delete(). Any instance returned by a get() or load() method is persistent. Detached instances may be made persistent by calling update(), saveOrUpdate(), lock() or replicate(). The state of a transient or detached instance may also be made persistent as a new persistent instance by calling merge(). save() and persist() result in an SQL INSERT, delete() in an SQL DELETE and update() or merge() in an SQL UPDATE. Changes to persistent instances are detected at flush time and also result in an SQL UPDATE. saveOrUpdate() and replicate() result in either an INSERT or an UPDATE. It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.

A Session instance is serializable if its persistent classes are serializable. A typical transaction should use the following idiom: Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); } If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.

1.1) First-level cache


First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means wont process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.

1.2) Second-level cache

Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will available to the entire application, dont bounds to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works. Here we can use query level cache also. Later we will discuss about it.

14) What are Callback interfaces?


These interfaces are used in the application to receive a notification when some object events occur. Like when an object is loaded, saved or deleted. There is no need to implement callbacks in hibernate applications, but theyre useful for implementing certain kinds of generic functionality.

15) What are Extension interfaces?


When the built-in functionalities provided by hibernate is not sufficient enough, it provides a way so that user can include other interfaces and implement those interfaces for user desire functionality. These interfaces are called as Extension interfaces.

sorted collection:
A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator. If your collection is not large, it will be more efficient way to sort it.

order collection :
Order collection is sorting a collection by specifying the order-by clause for sorting this collection when retrieval. If your collection is very large, it will be more efficient way to sort it .