SoftWare Used
1. Eclipse 3.2
2. Razorsql (http://www.razorsql.com/)
3 HsqlDB (http://hsqldb.org/)
4 JDK 1.6
5 ant
The following jar are requied to run the appication
1.hibernate3.jar
2.antlr-2.7.6.jar
3.javassist-3.9.0.GA.jar
4.jta-1.1.jar
5.dom4j-1.6.1.jar
6.commons-collections-3.1.jar
7.slf4j-api-1.5.8.jar
8.slf4j-simple-1.5.2.jar
9.hsqldb.jar
Most of the jars will be available in hibernate binary distribution, remaining can be downloaded from findjar.com
Below are the steps to develop and run the hibernate hsql Application
Create the directory structure and source files as shown below
JAVA SOURCE FILES
User.java
package com.upog.demo;
import java.util.Date;
public class User {
private int id;
private String name;
private Date date;
public User() {}
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HibernateUtil.java
package com.upog.demo;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory()
{
try
{
return new Configuration().configure().buildSessionFactory();
}
catch (Exception e)
{
System.out.println(” SessionFactory creation failed” + e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
HibernateTest.java
package com.upog.demo;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateTest {
public static void RetrieveUser()
{
System.out.println(“Retrieving User list from USER_INFO ….”);
Session session = HibernateUtil.getSessionFactory().openSession();
List UserList = session.createQuery(“from User”).list();
for (Iterator iterator = UserList.iterator(); iterator.hasNext();)
{
User user = (User) iterator.next();
System.out.println(user.getName() + “\t ” + user.getId() + “\t ” + user.getDate());
}
session.close();
}
public static void saveUser( String title)
{
Session session = HibernateUtil.getSessionFactory().openSession();
User user = new User();
user.setName(title);
user.setDate(new Date());
System.out.println(“\n Saving user ” + user.getName());
session.save(user);
session.flush();
session.close();
}
public static void main (String args[])
{
saveUser(“abc”);
saveUser(“def”);
saveUser(“Hi”);
saveUser(“hello”);
RetrieveUser();
}
}
Hibernate Configuration Files
Hibernate.cfg.xml - Contains the information about the database like URL,diver,ID,Password etc
Note: Change the value of connection.url as per you project home (I have given absolute path)
”-//Hibernate/Hibernate Configuration DTD 3.0//EN”
”http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd“>
org.hsqldb.jdbcDriver
jdbc:hsqldb:file:D:\data\workspace\Hibernate\database\mydb;shutdown=true
sa
2
org.hibernate.dialect.HSQLDialect
true
update
thread
Hibernate.hbm.xml – Defines the mapping between the Java Object and database table
”-//Hibernate/Hibernate Mapping DTD 3.0//EN”
”http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>
Create USER_INFO table in hsqldb
1. Connect hsqldb using razor sql with the properties as given below.
Login : sa
Password :
Driver class : org.hsqldb.jdbcDriver
Driver location :${PROJECT_HOME}\lib\hsqldb.jar
JDBC URL :jdbc:hsqldb:file:${PROJECT_HOME}\database\mydb;shutdown=true
2. Execute the following command
CREATE MEMORY TABLE PUBLIC.USER_INFO(ID INTEGER DEFAULT 0 NOT NULL PRIMARY KEY,NAME VARCHAR(25),CREATED_DATE DATE)
3. close the connection
Build.xml
[
Right click on the build.xml file in eclipse Then Click on Run As – > Ant Buid.
Read more
1. Eclipse 3.2
2. Razorsql (http://www.razorsql.com/)
3 HsqlDB (http://hsqldb.org/)
4 JDK 1.6
5 ant
The following jar are requied to run the appication
1.hibernate3.jar
2.antlr-2.7.6.jar
3.javassist-3.9.0.GA.jar
4.jta-1.1.jar
5.dom4j-1.6.1.jar
6.commons-collections-3.1.jar
7.slf4j-api-1.5.8.jar
8.slf4j-simple-1.5.2.jar
9.hsqldb.jar
Most of the jars will be available in hibernate binary distribution, remaining can be downloaded from findjar.com
Below are the steps to develop and run the hibernate hsql Application
Create the directory structure and source files as shown below
JAVA SOURCE FILES
User.java
package com.upog.demo;
import java.util.Date;
public class User {
private int id;
private String name;
private Date date;
public User() {}
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HibernateUtil.java
package com.upog.demo;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory()
{
try
{
return new Configuration().configure().buildSessionFactory();
}
catch (Exception e)
{
System.out.println(” SessionFactory creation failed” + e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
HibernateTest.java
package com.upog.demo;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateTest {
public static void RetrieveUser()
{
System.out.println(“Retrieving User list from USER_INFO ….”);
Session session = HibernateUtil.getSessionFactory().openSession();
List UserList = session.createQuery(“from User”).list();
for (Iterator iterator = UserList.iterator(); iterator.hasNext();)
{
User user = (User) iterator.next();
System.out.println(user.getName() + “\t ” + user.getId() + “\t ” + user.getDate());
}
session.close();
}
public static void saveUser( String title)
{
Session session = HibernateUtil.getSessionFactory().openSession();
User user = new User();
user.setName(title);
user.setDate(new Date());
System.out.println(“\n Saving user ” + user.getName());
session.save(user);
session.flush();
session.close();
}
public static void main (String args[])
{
saveUser(“abc”);
saveUser(“def”);
saveUser(“Hi”);
saveUser(“hello”);
RetrieveUser();
}
}
Hibernate Configuration Files
Hibernate.cfg.xml - Contains the information about the database like URL,diver,ID,Password etc
Note: Change the value of connection.url as per you project home (I have given absolute path)
”-//Hibernate/Hibernate Configuration DTD 3.0//EN”
”http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd“>
Hibernate.hbm.xml – Defines the mapping between the Java Object and database table
”-//Hibernate/Hibernate Mapping DTD 3.0//EN”
”http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>
Create USER_INFO table in hsqldb
1. Connect hsqldb using razor sql with the properties as given below.
Login : sa
Password :
Driver class : org.hsqldb.jdbcDriver
Driver location :${PROJECT_HOME}\lib\hsqldb.jar
JDBC URL :jdbc:hsqldb:file:${PROJECT_HOME}\database\mydb;shutdown=true
2. Execute the following command
CREATE MEMORY TABLE PUBLIC.USER_INFO(ID INTEGER DEFAULT 0 NOT NULL PRIMARY KEY,NAME VARCHAR(25),CREATED_DATE DATE)
3. close the connection
Build.xml
[
Right click on the build.xml file in eclipse Then Click on Run As – > Ant Buid.