TechnoBuzz

A Techno Blog, mainly about Java

Hibernate

Based on the hibernate.org example application, I came up with a servlet that writes to a db2 database. This uses the hibernate.properties file to specify the jdbc connection. I still would like to try getting the connection information using a JNDI context like is described here.

jars (place in build path):
cglib-full-2.0.1.jar
commons-logging-2.1.jar
hibernate2.jar
jta.jar
odmg-3.0.jar
commons-collections-1.0.3.jar
dom4j-1.4.jar
jdbc2_0-stdext.jar
log4j-1.2.8.jar

hibernate.properties:
hibernate.connection.driver_class = com.ibm.db2.jcc.DB2Driver
hibernate.connection.url = jdbc:db2://ipaddress:portnum/qms
hibernate.connection.username =
hibernate.connection.password =

Users.hbm.xml:
<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD//EN”
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd”&gt;
<hibernate-mapping>
<class name=”Users” table=”QMS.USERS”>
<id name=”name” type=”string” column=”NAME”> <generator class=”assigned”/> </id> 
<property name=”type” type=”integer” column=”TYPE”/>
</class>
</hibernate-mapping>

hibernate.cfg.xml:
<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 2.0//EN”
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd”&gt;
<hibernate-configuration>
<session-factory>
<property name=”hibernate.connection.driver_class”>com.ibm.db2.jcc.DB2Driver
</property>
<property name=”hibernate.connection.url”>jdbc:db2://ipaddress:portnum/qms
</property>
<property name=”hibernate.connection.username”>db2inst1</property>
<property name=”hibernate.connection.password”></property>
<property name=”dialect”>net.sf.hibernate.dialect.DB2Dialect</property>
<property name=”show_sql”>true</property>
<property name=”transaction.factory_class”>
net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<property name=”hibernate.cache.provider_class”>
net.sf.hibernate.cache.HashtableCacheProvider</property>
<property name=”hibernate.hbm2ddl.auto”>update</property>
<mapping resource=”Users.hbm.xml”/>
</session-factory>
</hibernate-configuration>

Database table:

CREATE TABLE qms.users ( 
NAME VARCHAR(32 ) NOT NULL PRIMARY KEY 
,TYPE INTEGER
) ;


public class Users 

   private String name; 
   private int type;
   public int getType() {
return type;

   public String getName() {
return name;

   public void setType(int type) {
this.type = type;

   public void setName(String i) {
name = i;
}
 }


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.Query;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;
public class AdminServlet extends HttpServlet {
SessionFactory _sessions;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<head>”);
    pw.println(“<meta http-equiv=\”Content-Type\” content=\”text/html;
charset=ISO-8859-1\">");
    pw.println("<title>Hibernate example</title>”);
    pw.println("</head>”);
    pw.println();
    pw.println("<body>”);
    pw.println();
    pw.println("<h1>Hibernate example</h1>”);
    pw.println();
    try {
            int i =
do_hibernate();
    } catch (HibernateException e) {
           e.printStackTrace();
    }
}
public void configure() throws HibernateException {
_sessions = new Configuration()
.addClass(Users.class)
.buildSessionFactory();
}
public int do_hibernate() throws HibernateException {
    System.out.println("start");
    configure()
    Session session = _sessions.openSession();
    Transaction tx = null;
    try {
           tx = session.beginTransaction();
           Users u = new Users();
           u.setName("jay@aol.com");
          u.setType(1);
          session.save(u);
          tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
    }
    return 0;
}
}

Hibernate API
Hibernate Guide
Mapping
Object to Relational Mapping and Relationships mapping using hibernate
Just looking for some examples on hibernate.
The first example comes from Aaron Johnson

with DB2

properties file

A new book is out called Hibernate – A developers notebook

A blog In relation to…

The Kickstart Guide

Mike Gloegl

Learn Hibernate

Developer Works

and of course Hibernate.org
hibernatedownload

wiki

plugin

Hibernate Sync plugin. To install, go to the install/update perspective , right click, new, enter url of
http://www.binamics.com/hibernatesync/eclipse2.1/site.xml, then on lower panel go down 3 levels,
and single click the item, on right side should see a button thats says install. I did this with wsad 5.1.2.

This article mentions the plugin.

Hibernate with xdoclet tutorial.

Advertisements

July 9, 2004 - Posted by | Hibernate & ORM

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: