Domain java files from hibernate mapping files

This post is related to the discussion on hibernate mappings below.
http://cuppajavamattiz.com/2012/01/20/hibernate-mappings-with-hbm-xml-files/
To take the discussion forward I have created two projects and I shall be dealing with each project in two separate posts.
This post is related to how the hbm2java task creates domain objects mapped to the database on the fly, from hbm.xml files.
The project structure on Eclipse looks like this:

<project>
     <generated/src>
          author.hbm.xml
          book.hbml.xml
          <com.mattiz.persistence.beans>
                 <classes generated by hbm2java.....>
                 BooksWithMultipleAuthors.java
                 ContributingAuthors.java
build.xml
hibernate.properties
log4j.properties
<classpath - jars>
      commons-collections.jar
      commons-logging.jar
      dom4j-1.6.1.jar
     freemarker.jar
     hibernate3.jar
     log4j-1.2.15.jar
     hibernate-annotation.jar
     xercesImpl.jar
     xml-apis.jar
     hibernate-tools.jar

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project default="codegen">
	<target name="codegen">
		<path id="classpath_id">
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/jakarta-commons/commons-collections.jar" />
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/jakarta-commons/commons-logging.jar" />
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/dom4j/dom4j-1.6.1.jar" />
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/freemarker/freemarker.jar" />
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/hibernate/hibernate3.jar" />
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/log4j/log4j-1.2.15.jar" />
			<pathelement
				location="I:/>packages/spring-framework-2.5.4-with-dependencies/spring-framework-2.5.4/lib/hibernate/hibernate-annotations.jar" />
			<pathelement location="I:/>packages/hibernate-tools.jar/hibernate-tools.jar" />
			<pathelement location="I:/>packages/hsqldb-1_8_0_10.jar" />
		</path>
		<echo>Zippzip</echo>
		<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask">
			<classpath refid="classpath_id" />
		</taskdef>
		<hibernatetool destdir="generated/src">
			<configuration propertyfile="./hibernate.properties">
				<fileset dir=".">
					<include name="generated/src/*.hbm.xml" />
				</fileset>
			</configuration>
			<hbm2java />
		</hibernatetool>
	</target>
</project>

author.hbml.xml

<?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 package="com.mattiz.persistence.beans">
	<class name="ContributingAuthor" table="Author" lazy="false">
		<id name="bookId" column="author_id" type="long">
			<generator class="native" />
		</id>
		<property name="firstName" type="string" length="20" column="first_name" />
		<property name="lastName" type="string" length="20" column="last_name" />
		<many-to-one name="bookContributedToByAuthor" column="isbn_code_in_author"
			class="BookWithMultipleAuthors" not-null="true" lazy="false" />
	</class>
</hibernate-mapping>

book.hbm.xml

<?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 package="com.mattiz.persistence.beans">
	<class name="BookWithMultipleAuthors" table="Book" lazy="false">
		<id name="isbnCode" column="isbn_code" type="long">
			<generator class="native" />
		</id>
		<property name="title" type="string" length="20" column="book_title" />
		<set name="contributingAuthors" inverse="true" lazy="false">
			<key column="isbn_code_in_author" />
			<one-to-many class="ContributingAuthor" />
		</set>
	</class>
</hibernate-mapping>

hibernate.properties

hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc\:hsqldb\:hsql\://localhost
hibernate.connection.username=user
hibernate.connection.password=user
hibernate.connection.pool_size=2
hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect

log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
log4j.logger.net.sf.hibernate=info
### log just the SQL
log4j.logger.net.sf.hibernate.SQL=debug
### log JDBC bind parameters ###
log4j.logger.net.sf.hibernate.type=info
### log schema export/update ###
log4j.logger.net.sf.hibernate.tool.hbm2ddl=debug
### log cache activity ###
log4j.logger.net.sf.hibernate.cache=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
log4j.logger.net.sf.hibernate.connection.DriverManagerConnectionProvider=trace

When you run build.xml, the following two classes are generated by hbm2java utility

package com.mattiz.persistence.beans;

// Generated 31 Jan by Hibernate Tools 3.2.2.GA
import java.util.HashSet;
import java.util.Set;

/**
 * BookWithMultipleAuthors generated by hbm2java
 */
public class BookWithMultipleAuthors implements java.io.Serializable {
	private long isbnCode;
	private String title;
	private Set contributingAuthors = new HashSet(0);

	public BookWithMultipleAuthors() {
	}

	public BookWithMultipleAuthors(String title, Set contributingAuthors) {
		this.title = title;
		this.contributingAuthors = contributingAuthors;
	}

	public long getIsbnCode() {
		return this.isbnCode;
	}

	public void setIsbnCode(long isbnCode) {
		this.isbnCode = isbnCode;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Set getContributingAuthors() {
		return this.contributingAuthors;
	}

	public void setContributingAuthors(Set contributingAuthors) {
		this.contributingAuthors = contributingAuthors;
	}
}
package com.mattiz.persistence.beans;

// Generated 31 Jan by Hibernate Tools 3.2.2.GA
/**
 * ContributingAuthor generated by hbm2java
 */
public class ContributingAuthor implements java.io.Serializable {
	private long bookId;
	private String firstName;
	private String lastName;
	private BookWithMultipleAuthors bookContributedToByAuthor;

	public ContributingAuthor() {
	}

	public ContributingAuthor(BookWithMultipleAuthors bookContributedToByAuthor) {
		this.bookContributedToByAuthor = bookContributedToByAuthor;
	}

	public ContributingAuthor(String firstName, String lastName,
			BookWithMultipleAuthors bookContributedToByAuthor) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.bookContributedToByAuthor = bookContributedToByAuthor;
	}

	public long getBookId() {
		return this.bookId;
	}

	public void setBookId(long bookId) {
		this.bookId = bookId;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public BookWithMultipleAuthors getBookContributedToByAuthor() {
		return this.bookContributedToByAuthor;
	}

	public void setBookContributedToByAuthor(
			BookWithMultipleAuthors bookContributedToByAuthor) {
		this.bookContributedToByAuthor = bookContributedToByAuthor;
	}
}

Check this download for a similar app here

Or download source code here

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: