Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to use hbm2cfgxml to generate Hibernate JPA configuration file

DZone's Guide to

How to use hbm2cfgxml to generate Hibernate JPA configuration file

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Hibernate Tools are used as the basis for both Ant tasks and Eclipse plugins, both available from tools.hibernate.org. In this post, you can see a complete Ant script that uses the <hbm2cfgxml> (Hibernate Configuration File) exporter to generate a hibernate.cfg.xml in JPA style. As a prerequisite, we have a META-INF/persistence.xml file in a folder named properties:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="unitTest">
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/usersdb"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>

Based on this file, we have created the below Ant script that generates the hibernate.cfg.xml configuration file (the file is generated into a folder, named hbm2cfgjpa_gen):

<project name="Hibernate Tools for Ant - hbm2cfg_jpa" default="generate">

<!-- Hibernate Tools libraries -->
<property name="librariesdir" value="./libs"/>

<!-- Hibernate Tools libraries paths -->
<path id="libraries">
  <fileset dir="${librariesdir}">
   <include name="*.jar"/>
  </fileset>
</path>

<!-- Directory where the generated .cfg.xml file is saved -->
<property name="build.dir.hbm2cfg_jpa" value="./hbm2cfgjpa_gen"/>

<!-- Define the "hibernatetool" task -->   
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" 
                                                    classpathref="libraries" />

<!-- Clean the exiting CFG files -->
<target name="clean">
 <delete includeemptydirs="true">
  <fileset dir="${build.dir.hbm2cfg_jpa}" includes="**/*"/>
 </delete>
</target>

<!-- Generating hibernate configuration file via hbm2cfgxml from a basic
    persistence.xml -->
<target name="generate" depends="clean">
<hibernatetool destdir="${build.dir.hbm2cfg_jpa}">
<classpath>
  <path location="./persistence"/>
 </classpath>
 <jpaconfiguration persistenceunit="unitTest"/>
 <hbm2cfgxml ejb3="${ejb3}" />
</hibernatetool>
</target>
</project>

The Ant script is named hbm2cfg_jpa.xml. It runs from command line prompt by:
${your_location}>ant –f hbm2cfg_jpa.xml

A possible output is listed below:

<?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.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.autocommit">true</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.release_mode">auto
        </property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/usersdb
        </property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.ejb.discard_pc_on_close">false</property>
        <property name="hibernate.query.jpaql_strict_compliance">true</property>
        <property name="hibernate.transaction.factory_class">
                       org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="hibernate.transaction.flush_before_completion">false
        </property>
        <property name="hibernate.use_identifier_rollback">false</property>
    </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-use-hbm2cfgxml-to-generate_22.html

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}