SQL Zone is brought to you in partnership with:

Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

How to generate DAO classes for Hibernate with JDBC transaction factory

04.02.2011
| 8180 views |
  • submit to reddit

If you need to generate DAO classes (Home classes) for Hibernate based on JDBC transaction factory, then in a start folder you need:

• A folder named hbm2java_gen that contains all the POJOs classes.
• The below ant script (named hbm2daoJNDI_JDBCTransactionFactory.xml):


<project name="Hibernate Tools for Ant - hbm2daoJDBC" default="run">
<target name="clean">
<delete dir="JDBC_java/classes"/>
<delete dir="JDBC_java/jar"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="JDBC_java/classes"/>
<javac srcdir="JDBC_java/src" destdir="JDBC_java/classes"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="JDBC_java/jar"/>
<jar destfile="JDBC_java/jar/GenerateDAOForJDBC.jar"
basedir="JDBC_java/classes">
<manifest>
<attribute name="Main-Class"
value="com.hibernate.jdbc.GenerateDAOForJDBC"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="JDBC_java/jar/GenerateDAOForJDBC.jar" fork="true"/>
</target>
</project>

• The JDBC_java folder that can be downloaded from http://www.filefactory.com/file/ca0a5c0/n/HibernateTools.rar.
• A folder named hbm2daojdbc_gen folder that will contain the generated DAOs.

Now, you can generate the DAOs, like this:

1. Edit /JDBC_java/src/package.properties file, to specify your application package (by default, it is set to com.hibernate.demo).
2. Run the above Ant script with:

ant –f hbm2daoJNDI_JDBCTransactionFactory.xml

A generated transaction will be of form (supposing a User POJO class):
public void persist(Users transientInstance) {
log.debug("Persisting Users instance");
try {
sessionFactory.getCurrentSession().beginTransaction();
sessionFactory.getCurrentSession().persist(transientInstance);
sessionFactory.getCurrentSession().getTransaction().commit();
log.debug("Persist successful");
} catch (RuntimeException re) {
factory.getCurrentSession().getTransaction().rollback();
log.error("Persist failed", re);
throw re;
}
}



For Tomcat users, in the JDBC_java/src/ you can find a simple example of web.xml and META-INF/context.xml for configuring datasource connection through JNDI.

A possible hibernate.cfg.xml for this case will look like below:
code>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.datasource">java:/comp/env/jdbc/usersdb
  </property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.transaction.factory_class">
       org.hibernate.transaction.JDBCTransactionFactory</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        
  <mapping resource="com/hibernate/demo/Users.hbm.xml" />
 </session-factory>
</hibernate-configuration>

This post is just one of a set of Ant scripts that explores Hibernate Tools for Ant. The complete set can be downloaded from here ( http://www.filefactory.com/file/ca0a5c0/n/HibernateTools.rar ).

 

From http://e-blog-java.blogspot.com/2011/03/how-to-generate-dao-classes-for.html

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: