Hibernate Core API
Following section covers the various core interfaces/classes that should be used by an application which is depending on the persistence-level service from Hibernate.
6.1) The ‘Configuration’ class
The
specific configuration that provides persistence services. This class provides convinient ways to import the Hibernate Configuration File and the necessary Mapping Files (which are discussed later) into an application which can be later used by
(which is detailed in the next section) class to create
Configuration class is the initial entry point of a Hibernate application. An instance ofConfiguration class represents applicationspecific configuration that provides persistence services. This class provides convinient ways to import the Hibernate Configuration File and the necessary Mapping Files (which are discussed later) into an application which can be later used by
SessionFactory(which is detailed in the next section) class to create
Session objects.The following code shows how to create and configure a Configuration object.
Configuration configuration = new Configuration().configure();
Calling the
Configuration.configure() method will configure the object by loading the configuration file and the mapping files that are available in the application’s class-path.Usually only one instance of a
Configuration class will be maintained by an application.6.2) The ‘SessionFactory’ interface
The
how to create a
Configuration class can be used to create SessionFactory object which serves as a factory class that creates Session objects (discussed in the next section). An application may have oneSessionFactory class but many number of Session objects. The following code showshow to create a
SessionFactory object by calling the Configuration.buildSessionFactory().Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory();
Configuration.buildSessionFactory() will create a new SessionFactory object with configurations and the mapping information taken from the Hibernate Configuration and the Mapping Files.6.3) The ‘Session’ interface
A Hibernate
Session interface forms the primary interface for promoting persistence services to the application classes. The following code shows how to create a Session object.Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session newSession = sessionFactory.openSession();
Calling the
SessionFactory.openSession() method, will create a new database connection (the connection related information like the Database server, username, password etc., will be taken the Hibernate configuration files which is discussed in the later section) and opens a new session from the created connection. Calling the SessionFactory.openSession() method again will create new Session objects as well as new java.sql.Connection object. One can get a reference to the newly created connection by calling the Session.connection() method, like the following,java.sql.Connection connection = session.connection();
6.3.1) Object Classification based on States
Three different types of objects are categorized in Hibernate technology based on the state of the object. They are,
- Persistent Object
- Transient Object
- Detached Object
Persistent Object:
A persistent object is one which is associated with the current Session and has its state values synchronized with the database.
Transient Object:
A transient object doesn’t have any association with the current Session, but can be made persistent at a later time.
Detached Object:
A detached object was having an association at a previous point of time with the Session interface, but now is made to detach (taken-off) from the current Session.
No comments:
Post a Comment