Thursday, 2 August 2012

Hibernate–Java Environment setup

We will see how we can setup the Environment in eclipse to learn our examples in hibernate.

Environment

  1. Eclipse 3.7 Indigo IDE Download
  2. Hibernate 4.1.1 Download
  3. JavaSE 1.6  Download
  4. MySQL 5.1 Download
  5. JDBC Driver for MySQL ((MySQL-connector-java-5.1.19.zip)) Download
  6. Extract the Hibernate, MySql driver files in a separate folder
Step 1:
  1. In the eclipse, Click new –> Java Project
  2. Enter the Project name <xxxxx>
  3. Click Next
  4. Goto Libraries Tab as shown

image

5. Click On Add External Jar files and add the following files.

  • 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

5. Click finish button.

Step 2:
Now let us create hibernate utility class which we can use to save, update and delete records in the database.
  1. Expand the src folder and Right click and Select New Class
  2. Enter the Package Name as HibernateUtilities and Class Name as HibernateUtil and leave all the other values as default
  3. Now you can paste the below 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();
}

}


Step 3:
Now let us create the hibernate.cfg.xml file where we will configure all our Database information

Select the SRC Folder and Right click and Select New –> File. Give the file name as hibernate.cfg.xml and Click Finish Button.
Goto Source view and paste the following code. Please change Database name, user id and password according inly.


<?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/sampledb</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>


That’s all.

No comments:

Post a Comment