Friday, April 16, 2010

How to Run Hibernate

There are couple of jars that are needed to run the Hibernate Applications. Some jars from third party and some are from JBoss community.
Jars:
• antlr.jar
• backport-util-concurrent.jar
• hibernate-commons-annotations.jar
• hibernate-annotations.jar
• hibernate-ehcache.jar
• log4j.jar
• slf4j-log4j12.jar
• slf4j-api.jar
• javassist.jar
• commons-collections.jar
• dom4j.jar
• lucene-core.jar
• commons-logging.jar
• hibernate-search.jar
• jta.jar
• hibernate-core.jar
• ehcache.jar
• xml-apis.jar

those mentioned jars can be download from respective sites or from
Jar Finder

To create Hybernate Application,
Step1 : You need to add all those jars into your application class path.
Step2 : Create hibernate configuration file (hibernate.cfg.xml).
file will contain hibernate runtime properties of Session factory eg. "driver-class" ,"database-url", "user","password" etc. and hibernate mapping resources.
eg.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/ioffice
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml" />
</session-factory>
</hibernate-configuration>

Step3 : Create mapping file like <class-name>.hbm.xml, the content will be like:
eg.
<?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="Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>

Step4 : Create a class named "Contact.java"

public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;

public String getEmail() {
return email;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setEmail(String string) {
email = string;
}

public void setFirstName(String string) {
firstName = string;
}

public void setLastName(String string) {
lastName = string;
}

public long getId() {
return id;
}

public void setId(long l) {
id = l;
}

@Override
public String toString() {
return this.id + ", " + this.firstName + ", " + this.lastName
+ ", " + this.email;
}
}

Step5: Use
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class FirstExample {
private SessionFactory sessionFactory = null;

public FirstExample() {
sessionFactory = new Configuration().configure().buildSessionFactory();
}

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public void save(Contact contact)
{
Session session = null;
try {
session = this.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
System.out.println("Inserting Record");
session.save(contact);
tr.commit();
System.out.println("Done");
}
catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
}

public void delete(Contact contact)
{
Session session = null;
try {
session = this.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
System.out.println("Deleting Record.");
session.delete(contact);
tr.commit();
System.out.println("Done");
}
catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
}

public void update(Long key, Contact contact)
{
Session session = null;
try {
session = this.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
System.out.println("Updating Record.");
Contact cntct = (Contact) session.get(Contact.class, key);
cntct = contact;
session.update(cntct);
tr.commit();
System.out.println("Done");
}
catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
}

public Contact fetch(Long key)
{
Session session = null;
Contact contact = null;
try {
session = this.getSessionFactory().openSession();
System.out.println("Query Record.");
contact = (Contact) session.get(Contact.class, key);
System.out.println("Done");
}
catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
return contact;
}

public static void main(String[] args) {
FirstExample fEx = new FirstExample();
Contact cont = fEx.fetch(new Long(4));
System.out.println(cont);
}
}

No comments:

Post a Comment