Академический Документы
Профессиональный Документы
Культура Документы
Configuration (class)
ServiceRegistry (interface)
SessionFactory (interface)
Session (interface)
Transaction (interface)
Query (interface)
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();
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();
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();
both methods are used to create records inside database. Both methods are available inside Session
interface,
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
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,
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,
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.