Hibernate Create session

Source: Internet
Author: User

1. Create a Sessionfactory object
Sessionfactory sessionfactory = null;

1). To create a Configuration object: Basic configuration information and object-relational mapping information corresponding to hibernate
Configuration configuration = new configuration (). Configure ();

4.0 before creating this
Sessionfactory = Configuration.buildsessionfactory ();

2). Create a Serviceregistry object: Hibernate 4.x newly added object
Any configuration and service of hibernate needs to be registered in the object before it can be valid.
Serviceregistry Serviceregistry =
New Serviceregistrybuilder (). Applysettings (Configuration.getproperties ())
. Buildserviceregistry ();

3).
Sessionfactory = Configuration.buildsessionfactory (serviceregistry);

2. Create a Session object
Session session = Sessionfactory.opensession ();

3. Open transaction
Transaction Transaction = Session.begintransaction ();

Using Hibernate for data persistence, there are usually the following steps:
Write Persistence class: POJO + Map File
Get the Configuration object
Get Sessionfactory Object
Get Session, open transaction
Manipulating databases in an object-oriented manner
Close a transaction, close the Session

The configuration class is responsible for managing Hibernate config information. Include the following:
Hibernate runs the underlying information: database URL, user name, password, JDBC driver class, database dialect, database connection pool, etc. (corresponding to Hibernate.cfg.xml file).
The mapping of persisted classes to data tables (*.hbm.xml file)
Two ways to create a Configuration
Properties File (hibernate.properties):
Configuration cfg = new configuration ();
XML file (hibernate.cfg.xml)
Configuration cfg = new configuration (). Configure ();
The Configure method of the Configuration also supports access with parameters:
File File = new file ("Simpleit.xml");
Configuration cfg = new configuration (). Configure (file);

The compiled memory image for a single database mapping relationship is thread-safe.
Once the Sessionfactory object is constructed, specific configuration information is given
Sessionfactory is the factory that generated the session
Construction sessionfactory consumes resources, and in general, only one Sessionfactory object is initialized in an application.
Hibernate4 added a serviceregistry interface, all Hibernate-based configurations or services must be unified to this serviceregistry to take effect after registration
Steps to create sessionfactory in Hibernate4


A session is a single-threaded object that interacts between an application and a database and is the center of Hibernate operations, and all persistent objects must be managed by the session to be persisted. This object has a short life cycle. The session object has a first-level cache, and the data for all persisted layer operations is slowed down until the session object is persisted until flush is explicitly performed. Equivalent to Connection in JDBC.


Persistent classes have the ability to persist after they are associated with the Session.
Method of Session class:
Methods for getting persisted objects: get () load ()
Persisted objects have to be saved, updated and deleted: Save (), update (), Saveorupdate (), delete ()
Open transaction: BeginTransaction ().
Method of managing Session: IsOpen (), flush (), clear (), evict (), close (), etc.

Hibernate Create session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.