1

I was wondering if it was possible to generate entities from hibernate mapping file. My ultimate goal is to programmaticaly create a mapping file, and then create the database from it. I know that I can use :

<prop key="hibernate.hbm2ddl.auto">create</prop>

To create database form entities, but is there a way, (without eclipse tool, I need to do it automatically in the application) to generate entities from mapping? (Or even directly database from mapping).

I don't know if other tools can do it, but I though I would use Hibernate because of the cross database compatibility.

Thank you! Guillaume

1
  • Just too add more information, I will never have use of entities in the application, I just need to generate a database, based on an other database, describing a database schema. Commented Aug 29, 2014 at 9:05

1 Answer 1

2

You can use maven with the help of mojo plugins to generate Java entities by providing your hibernate mapping files.

Here is a sample maven project:

Create your maven project of this structure:

¦pom.xml
¦
+---src
¦   +---main
¦       +---java
¦       +---resources
¦           ¦   hibernate.cfg.xml
¦           ¦
¦           +---hbmFiles
¦                   Person.hbm.xml

The hibernate configuration file hibernate.cfg.xml is placed in YourProject/src/main/java/resources

The mapping files should be placed in resources or sub-directory of resources folder.

Contents of Person.hbm.xml file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mypackage.Person" table="person">
   <id name="id" column="id" type="int"/>
   <property name="name" column="name" type="string"/>
</class>
</hibernate-mapping>

Contents of hibernate.cfg.xml file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping resource="hbmFiles/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Contents of pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.hibernate.tutorials</groupId>
  <artifactId>MyProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>MyProject</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
     <plugins>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>hibernate3-maven-plugin</artifactId>
           <version>2.2</version>
           <executions>
             <execution>
              <phase>default</phase>
              <goals>
                <goal>hbm2java</goal>
              </goals>
             </execution>
            </executions>
           <configuration>
             <components>
              <component>
               <name>hbm2java</name>
               <implementation>configuration</implementation>
               <outputDirectory>generated-sources/hibernate3</outputDirectory>
              </component>
             </components>
             <componentProperties>
              <drop>true</drop>
              <ejb3>true</ejb3>
              <jdk5>true</jdk5>
             </componentProperties>
           </configuration>
        </plugin> 
     </plugins>
  </build>
</project>

The property - ejb3=true is useful if you need annotations in your generated java entity files. If annotations are not required then you can just remove the line - <ejb3>true</ejb3>

After creating these 3 file, you can run the command:

mvn clean hibernate3:hbm2java

Now maven generates the java entity files at path -

YourProject/generated-sources/hibernate3/..package_mentioned_in_hbm_files../Student.java

Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect! I had no idea about that pugin. Thank's a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.