Configuring sessionfactory in your java program, Specifying the mapping resources, Closing the session – HP Integrity NonStop J-Series User Manual

Page 188: Database transaction management

Advertising
background image

Configuring SessionFactory in your Java Program

Add the following lines to your Java program to create Hibernate SessionFactory.

private SessionFactory factory;

...
...
...
public void databaseCon()
{
Configuration cfg = new Configuration();
factory = cfg.buildSessionFactory();
...
...
...
}

Creating a New Session from the SessionFactory in Java

Add the following lines to your Java program to create a new session from the SessionFactory:

Session s = factory.openSession();

This will create the session which will be used further to perform database operation.

NOTE:

To view the complete configuration snippet of the hibernate.properties file, see

“JDBC Configuration” (page 227)

.

Specifying the Mapping Resources

Specify all the mapping files (.hbm) in your Java program as shown:

Configuration cfg = new Configuration()
.addResource("<Name of mapping file#1>")
.addResource("<Name of mapping file#2>");

For example,

If the mapping file used in your application is Item.hbm.xml and Bid.hbm.xml, specify it as
shown:

Configuration cfg = new Configuration()
.addResource("Item.hbm.xml")
.addResource("Bid.hbm.xml");

The basic configuration is ready for you to perform database operation using Hibernate.

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();

Database Transaction Management

Hibernate works in any environment that uses JTA, in fact, we recommend to use JTA whenever
possible as it is the standard Java transaction interface. You get JTA built-in with all J2EE/JEE
application servers, and each datasource you use in such a container is automatically handled by
a JTA TransactionManager.

188 Configuring Hibernate Applications on NonStop Systems

Advertising
This manual is related to the following products: