Over a million developers have joined DZone.

How to generate DAO classes for Hibernate with JDBC transaction factory

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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 name="compile" depends="clean">
<mkdir dir="JDBC_java/classes"/>
<javac srcdir="JDBC_java/src" destdir="JDBC_java/classes"/>
<target name="jar" depends="compile">
<mkdir dir="JDBC_java/jar"/>
<jar destfile="JDBC_java/jar/GenerateDAOForJDBC.jar"
<attribute name="Main-Class"
<target name="run" depends="jar">
<java jar="JDBC_java/jar/GenerateDAOForJDBC.jar" fork="true"/>

• 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 {
log.debug("Persist successful");
} catch (RuntimeException re) {
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:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  <property name="hibernate.connection.datasource">java:/comp/env/jdbc/usersdb
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.transaction.factory_class">
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        
  <mapping resource="com/hibernate/demo/Users.hbm.xml" />

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

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}