Opening a session for database operation, Closing the session – HP Integrity NonStop J-Series User Manual
Page 109
![background image](/manuals/397993/109/background.png)
The basic configuration is ready for you to perform database operation using Hibernate.
Opening a Session for Database Operation
Opening a new session for database transaction involves:
1.
“Configuring SessionFactory in the hibernate.cfg.xml File” (page 109)
2.
“Creating a New Session from the SessionFactory in your Java Program” (page 109)
Configuring SessionFactory in the hibernate.cfg.xml File
Add the following lines to configure the Hibernate SessionFactory.
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
...
...
...
...
...
NOTE:
The above configuration uses the hibernate-configuration-3.0.dtd file.
Creating a New Session from the SessionFactory in your Java Program
Add the following lines of code in your Java program:
SessionFactory sessionFactory;
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
NOTE:
To view the complete configuration snippet of hibernate.cfg.xml, see
Configuration for Hibernate” (page 338)
.
Closing the Session
Once the database operation is complete, close the session to deallocate the memory.
Add the following line of code when you complete the database operation.
session.close();
Configuring JDBC Type 2 or Type 4 Driver for SQL/MX Database using hibernate.properties
To complete the database operation in a Hibernate application using the JDBC driver for the
SQL/MX database, complete the following steps:
1.
“Specifying the JDBC Driver Class for SQL/MX Database” (page 110)
2.
“Defining the Connection URL” (page 110)
3.
“Establishing the Connection” (page 110)
4.
“Defining the Hibernate Dialect for SQL/MX Database” (page 110)
5.
“Opening a Session for Database Operation” (page 110)
6.
“Specifying the Mapping Resources” (page 111)
7.
“Closing the Session” (page 111)
Configuring Hibernate Applications on NonStop Systems 109