Вы находитесь на странице: 1из 4

Hibernate is an Open source Object Relational Mapping framework.

It enables developer to develop


classes to be persist in an object oriented way including inheritance and the Java collections framework.
Current hibernate stable release is 4.3.5 that was released in April 2014.

core classes and interfaces of hibernate.

Configuration (class)
ServiceRegistry (interface)
SessionFactory (interface)
Session (interface)
Transaction (interface)
Query (interface)

Configuration: Is a class resides inside the org.hibernate.cfg package. Configuration as


it's name implies it is all about the configuration of the application, configuration of the application done
only at the initialization time which is only once for the life time of the project. Basically Configuration is
used to build the sessionFactory via loading the database related configuration details.
Syntax:
Configuration configuration = new Configuration();
configuration.configure();

ServiceRegistry: Is an interface resides inside the org.hibernate.service package.


ServiceRegistry is all about access management of services. To build ServiceRegistry instance you need
ServiceRegistryBuilder class.
Syntax:
ServiceRegistry serviceRegistry = new
ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

Update, From Hibernate 4.3 ServiceRegistryBuilder is deprecated. If you are using hibernate 4.3 then
use the below one instead of the above.
ServiceRegistry serviceRegistry = new
StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

SessionFactory: Is an interface available inside the org.hibernate package. It's all


about the factory of session, you can create and open a session from the SessionFactory. Basically an
application has a single sessionFactory instance which is a thread safe.
Syntax:
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Session: Is an interface available inside the org.hibernate package. The heart of the hibernate
is a session. It's a runtime interface between your application and hibernate. Session is a single threaded
short lived object, Session instances obtain from the SessionFactory.
Syntax:
Session session = sessionFactory.openSession();

Transaction: Is an interface available inside the org.hibernate package. Transaction as


it's name implies it allows the application to define there units of work into a single transaction. A
transaction is associated with Session.
Syntax:
session.beginTransaction();
session.save(user);
session.update(user_payrole);
session.getTransaction().commit();

Query: Is an interface available inside the org.hibernate package. Query is all about the object
oriented representation of hibernate query. A Query instance is obtained by calling
session.createQuery().
Syntax:
session.createQuery("from User").list();

Q) Difference between save and persist method in hibernate

both methods are used to create records inside database. Both methods are available inside Session
interface,

1. Return type of save() is Serializable. Here is complete signature of save() method,


Serializable save(Object object) throws HibernateException. Whereas return type of
persist() is void. Here is complete signature of persist() method, void persist(Object
object) throws HibernateException
2. The persist() makes a transient instance persistent. However, it does not guarantee that
the identifier value will be assigned to the persistent instance immediately, the
assignment might happen at flush time. Whereas save() makes a transient instance
persistent, and does guarantee to return an identifier. If an INSERT has to be executed to
get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens
immediately.
3. The persist() also guarantees that it will not execute an INSERT statement if it is called
outside of transaction boundaries. This is useful in long-running conversations with an
extended Session/persistence context. Whereas save() INSERT happens immediately, no
matter if you are inside or outside of a transaction. This is problematic in a long-running
conversation with an extended Session/persistence context.
Transient - an object is transient if it has just been instantiated using the new operator, and it is not
associated with a Hibernate Session . It has no persistent representation in the database and no
identifier value has been assigned.

Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the
database (i.e. to write changes to the database)..During Commit transaction

How to use:

long id = session.save(user);
session.persist(user); // this must be in transaction block

Q) What are the states of Hibernate instances?

Hibernate has 3 different types of object states, all three states are related to Hibernate
persistence context and these are listed below.

1. transient
2. persistent
3. detached

Transient:
An object is a transient object, if and only if object is just instantiated using new operator and it
is not associated with Hibernate session. There is no any persistence representation of the this
object in the database and no any identifier value is associated with the object.
For example,

// Here user is transient object.


User user = new User();

Persistent:
An object is in persistent state if it has the representation in the database with identifier value.
Persistent object has the scope in Session. Persistent object may be just inserted object in the
database with identifier value, and a row value representation in the database.
For example,

User user = new User();


Long savedID = (Long)session.save(user);

Detached:
The object was associated with persistence context, but the context was closed for now, or the
object was serialized to another process. A detached object can be reattached to a new session at
any point of time.

Вам также может понравиться