DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. How to use hbm2cfgxml to generate Hibernate JPA configuration file

How to use hbm2cfgxml to generate Hibernate JPA configuration file

A. Programmer user avatar by
A. Programmer
·
Mar. 23, 11 · Interview
Like (0)
Save
Tweet
Share
8.48K Views

Join the DZone community and get the full member experience.

Join For Free

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

Hibernate

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Application Architecture Design Principles
  • Microservices Testing
  • 11 Observability Tools You Should Know
  • Testing Level Dynamics: Achieving Confidence From Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: