Over a million developers have joined DZone.

How to generate DAO classes for Hibernate with JDBC transaction factory

· Java Zone

Learn more about how the Java language, tools and frameworks have been the foundation of countless enterprise systems, brought to you in partnership with Salesforce.

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

Discover how the Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services, brought to you in partnership with Salesforce.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}