This post will explain how to set up the Hibernate for a ZK Project
Environment
Step 1:
In the Eclipse, Select File –> New –> ZK Project and give Project Name as <AnyNameAsYouWish>
Step 2:
Copy the following the following files in <YourProjectName> –> WebContent –> Web-INF –> Lib Folder
- antlr-2.7.7.jar
- dom4j-1.6.1.jar
- hibernate-commons-annotations-4.0.1.Final.jar
- hibernate-core-4.1.1.Final.jar
- hibernate-entitymanager-4.1.1.Final.jar
- hibernate-jpa-2.0-api-1.0.1.Final.jar
- javassist-3.15.0-GA.jar
- jboss-logging-3.1.0.GA.jar
- jboss-transaction-api_1.1_spec-1.0.0.Final.jar
- mysql-connector-java-5.1.18-bin.jar
Step 3:
Create Hibernate Configuration File
Expand Java Resources Folder. Right Click on src folder and Select New->General->File and give the name as hibernate.cfg.xml
Paste the following code
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/drkare</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property><!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property><!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property><!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property><!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property><!-- Mapping Classes -->
</session-factory>
</hibernate-configuration>Please take care to change the DB Name, User and Password according your setup
Step 4:
Now let us create the hibernate utility class where we will establish the DB Configuration using the above configuration file
Right Click on Java Resource folder-> Select New –> Class. Give the Package name as HibernateUtilities and Class name as HibernateUtil.java
Paste the following code
package HibernateUtilities;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;public class HibernateUtil {
private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;public static Configuration getInitConfiguration() {
Configuration config = new Configuration();
config.configure();
return config;
}public static Session getSession() {
if (factory == null) {
Configuration config = HibernateUtil.getInitConfiguration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
factory = config.buildSessionFactory(serviceRegistry);
}
Session hibernateSession = factory.getCurrentSession();
return hibernateSession;
}public static Session beginTransaction() {
Session hibernateSession;
hibernateSession = HibernateUtil.getSession();
hibernateSession.beginTransaction();
return hibernateSession;
}public static void CommitTransaction() {
HibernateUtil.getSession().getTransaction().commit();
}public static void closeSession() {
HibernateUtil.getSession().close();
}public static void rollbackTransaction() {
HibernateUtil.getSession().getTransaction().rollback();
}}