RSS

Creating Java domain objects with the Maven Hibernate plugin and Hibernate tools.

08 May

Visit my new website http://java4developers.com

This is the second way we mentioned in this parent post of creating java domain objects, or data access objects, mapping files. The Hibernate 3 Maven plugin webpage show us which goals are available for this plugin. In this post we are going to use the following four ones:

  • hibernate3:hbm2cfgxml: Generates hibernate.cfg.xml.
  • hibernate3:hbm2hbmxml: Generates a set of hbm.xml files.
  • hibernate3:hbm2java: Generates Java domain objects.
  • hibernate3:hbm2dao: Generates data access objects.


So, what we need to do is creating a new Maven project in either Eclipse or STS and add to our pom.xml file these goals. It is pretty simple. First we add the Hibernate 3 Maven plugin to the pom.xml

<plugin>
		<groupId>org.codehaus.mojo</groupId>
		<artifactId>hibernate3-maven-plugin</artifactId>
		<version>2.2</version>
		<executions>
                                  <execution></execution>
                                  <execution></execution>
                                  <execution></execution>
                                  <execution></execution>
                 </executions>
</plugin>

It will be 4 different executions, one for each Maven Hibernate 3 goal we have to launch.

Hibernate3:hbm2cfgxml: Generates hibernate.cfg.xml.
.
This is a little bit tricky because we need to create a reveng file to launch this goal. If we don´t want to do this, another thing to do is creating a simple hibernate.cfg.xml. It is sound weird, but we have to create a hibernate.cfg.xml to create a new hibernate.cfg.xml. The difference between the old and the new one is that the hibernate.cfg.xml generated with the Hibernate 3 Maven plugin from the old hibernate.cfg.xml created manually would include a entry for each table. So the new hibernate.cfg.xml looks like:

<?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.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_schema">test</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="Car.hbm.xml" />
    </session-factory>
</hibernate-configuration>

We can see that the goal is launch in the generate-resources phase.
The execution of the hbm2cfgxml will be:

<execution>
	<id>hbm2cfgxml</id>
	<phase>generate-resources</phase>
	<goals>
		<goal>hbm2cfgxml</goal>
	</goals>
	<inherited>false</inherited>
	<configuration>
		<components>
			<component>
				<name>hbm2cfgxml</name>
				<implementation>jdbcconfiguration</implementation>
			</component>
		</components>
		<componentProperties>
			<packagename>${packageName}</packagename>
		</componentProperties>
	</configuration>
</execution>

Using the new hibernate.cfg.xml file we are going to launch the other 3 goals.

hibernate3:hbm2hbmxml: Generates a set of hbm.xml files.

<execution>
	<id>hbm2hbmxml</id>
	<phase>generate-resources</phase>
	<goals>
		<goal>hbm2hbmxml</goal>
	</goals>
	<inherited>false</inherited>
	<configuration>
		<components>
			<component>
				<name>hbm2hbmxml</name>
				<outputDirectory>target/classes</outputDirectory>
			</component>
		</components>
		<componentProperties>
			<packagename>${packageName}</packagename>
		</componentProperties>
	</configuration>
</execution>
<execution>

hibernate3:hbm2java: Generates Java domain objects.
In our case we are going to generate java domain classes with annotations and jdk5 style. Notice that the java sources will be save in a package () created within the main/src/java folder. At the same time the hibernate.cfg.xml used is the one generated with the plugin, which is within target/hibernate3/generated-mappings/ folder. We can include a set of templates to modify the aspect of our java classes.

<execution>
	<id>hbm2java</id>
	<phase>test</phase>
	<goals>
		<goal>hbm2java</goal>
	</goals>
	<inherited>false</inherited>
	<configuration>
		<components>
			<component>
				<name>hbm2java</name>
				<implementation>annotationconfiguration</implementation>
				<outputDirectory>src/main/java</outputDirectory>
			</component>
		</components>
		<componentProperties>
			<packagename>${packageName}</packagename>
			<configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
<templatepath>src/main/resources/hbmtemplates</templatepath>
			<ejb3>true</ejb3>
			<jdk5>true</jdk5>
		</componentProperties>
	</configuration>
</execution>

hibernate3:hbm2dao: Generates data access objects.
Pretty much the same as with the hbm2java goal.

<execution>
	<id>hbm2dao</id>
	<phase>test</phase>
	<goals>
		<goal>hbmtemplate</goal>
	</goals>
	<inherited>false</inherited>
	<configuration>
		<components>
			<component>
				<name>hbmtemplate</name>
				<implementation>jdbcconfiguration</implementation>
				<outputDirectory>src/main/java</outputDirectory>
			</component>
		</components>
		<componentProperties>
			<packagename>${packageName}</packagename>
			<configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
			<templatepath>src/main/resources/hbmtemplates/</templatepath>
			<ejb3>true</ejb3>
			<jdk5>true</jdk5>
		</componentProperties>
	</configuration>
</execution>
 
5 comentarios

Publicado por en 8 May, 2011 en Hibernate

 

Etiquetas: , , , ,

5 Respuestas a “Creating Java domain objects with the Maven Hibernate plugin and Hibernate tools.

  1. Pingback: JavaPins
  2. 4view.me

    14 May, 2013 at 9:50 pm

    I blog frequently and I genuinely thank you for your information.
    This great article has truly peaked my interest.

    I will bookmark your blog and keep checking for
    new details about once a week. I opted in for your
    RSS feed as well.

     
  3. Kasey

    18 May, 2013 at 6:08 pm

    Heya i am for the first time here. I found this board and I find It really useful
    & it helped me out a lot. I hope to give something back and help
    others like you helped me.

     

Deja un comentario