Topic: Using Hibernate With Tooltwist
Hibernate is integrated with Tooltwist after the XPCs - or for the XPCs to utilize. This means that Hibernate is used only locally within the scope of a certain project.
In the Tutuki project, Hibernate was used as the persistence layer and to handle ORM (Object Relational Mapping). The Tooltwist XPCs can then make calls to the database using Hibernate.
To install Hibernate to a project within Tooltwist:
1. Download Hibernate (hibernate3.jar) from https://www.hibernate.org and add the jar to the project's classpath.
2. Wire your entity classes to the database. Hibernate offers Object Relational Mapping so that the application can interact with the database in an OOP manner. For e.g. if you have a database table called "promo" with fields:
"promoId" serial NOT NULL
"zoneName" character varying(255)
"url" character varying(255)
"textBlock" character varying(255)Create a class named "Promo" that implements java.io.Serializable. Match the Promo's attributes to the fields of the promo table:
public class Promo implements java.io.Serializable {
private Integer promoId;
private String zoneName;
private String url;
private String textBlock;
public Integer getPromoId() {
return promoId;
}
public void setPromoId(Integer promoId) {
this.promoId = promoId;
}
public String getZoneName() {
return zoneName;
}
public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTextBlock() {
return textBlock;
}
public void setTextBlock(String textBlock) {
this.textBlock = textBlock;
}
}The Promo class is a Plain Old Java Object (POJO) containing attributes with getters & setters.
Under the same package (or wherever you want to place it), create Promo.hbm.xml. This configuration file is what Hibernate will use to wire the Promo class to the promo table:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sinocode.godzillow.model.Promo" table="`Promo`"
schema="public">
<id name="promoId" type="java.lang.Integer">
<column name="`promoId`" />
<generator class="sequence">
<param name="sequence">"Promo_promoId_seq"</param>
</generator>
</id>
<property name="zoneName" type="java.lang.String">
<column name="`zoneName`" />
</property>
<property name="url" type="java.lang.String">
<column name="`url`"/>
</property>
<property name="textBlock" type="java.lang.String">
<column name="`textBlock`" />
</property>
</class>
</hibernate-mapping>3. Under the source folder, create a *.cfg.xml file named after your database (e.g. mydatabase.cfg.xml). This configuration provides the session factory for Hibernate to use. It tells Hibernate about the database connection, connection settings, pooling, and the ORM mapping files.
*Refer to http://docs.jboss.org/hibernate/core/3.
ation.html for more information about hibernate configuration
*Refer to https://www.hibernate.org/214.html for further explanation about c3p0 connection pooling.
Here is a snippet from the prd3godzillow.cfg.xml of the Tutuki project:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://10.1.1.153:5433/prd3Godzillow_1_15_6</property>
<property name="connection.username">postgres</property>
<property name="connection.password">vsc</property>
<!-- Use hibernate.c3p0 connection pool -->
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.timeout">100</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">0</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Call ORM mapping classes -->
<mapping resource="com/sinocode/godzillow/model/Promo.hbm.xml" />
</session-factory>
</hibernate-configuration>*Note: If you will use c3p0 for connection pooling, you will need to download the c3p0 library.
4. Create a Hibernate utility class (e.g. HibernateUtil.java) that will serve as a Hibernate session provider to the application.
HibernateUtil.java of Tutuki project:
package com.tutuki.util;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.sinocode.godzillow.util.PageList;
import org.hibernate.Query;
import org.hibernate.Session;
public class HibernateUtil {
private static final Logger logger = Logger.getLogger(HibernateUtil.class);
private static final SessionFactory appSessionFactory = buildAppSessionFactory();
private static final SessionFactory conSessionFactory = buildConSessionFactory();
public static void init() {}
private static SessionFactory buildAppSessionFactory() {
logger.info("Initializing appSessionFactory...");
try {
/* Create the SessionFactory from prd3godzillow.cfg.xml */
Configuration configuration = new Configuration();
configuration.configure("prd3godzillow.cfg.xml");
return configuration.buildSessionFactory();
} catch (Throwable ex) {
logger.info("Initial appSessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
private static SessionFactory buildConSessionFactory() {
logger.info("Initializing conSessionFactory...");
try {
/* Create the SessionFactory from prd3godzillow.cfg.xml */
Configuration configuration = new Configuration();
configuration.configure("content.cfg.xml");
return configuration.buildSessionFactory();
} catch (Throwable ex) {
logger.info("Initial conSessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getAppSessionFactory() {
return appSessionFactory;
}
public static SessionFactory getConSessionFactory() {
return conSessionFactory;
}
}This utility class provides Hibernate session from two databases: prd3godzillow and content database. Notice the lines:
Configuration configuration = new Configuration();
configuration.configure("prd3godzillow.cfg.xml");
return configuration.buildSessionFactory();and
Configuration configuration = new Configuration();
configuration.configure("content.cfg.xml");
return configuration.buildSessionFactory();5. You can now query your database from your XPCs using Hibernate:
Session session = HibernateUtil.getAppSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("SELECT p FROM Promo p");
List<Promo> widgets = query.list();*The code above uses HQL or Hibernate Query Language. Please refer to https://www.hibernate.org/hib_docs/nhib ryhql.html for more information about HQL.
