Hi all,
I was wondering if anyone could help me with this. I'm trying to generate toString, hashCode and equals in my pojos when I do my reveng.
Sadly, all I seem to get is the following... :(
Code:
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
buffer.append("]");
return buffer.toString();
}
public boolean equals(Object other) {
boolean equalsValue = false;
if(this == other){
equalsValue = true;
}
if(other == null){
equalsValue = false;
}
if(!(other instanceof Game)){
equalsValue = false;
}
Game castOther = (Game)other;
equalsValue = false;
return equalsValue;
}
public int hashCode() {
int result = 17;
return result;
}
Here are the templates I'm using:
Code:
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
<#foreach property in pojo.getToStringPropertiesIterator()> buffer.append("${property.getName()}").append("='").append(${pojo.getGetterSignature(property)}()).append("' ");
</#foreach> buffer.append("]");
return buffer.toString();
}
public boolean equals(Object other) {
boolean equalsValue = false;
if(this == other){
equalsValue = true;
}
if(other == null){
equalsValue = false;
}
if(!(other instanceof ${pojo.getDeclarationName()})){
equalsValue = false;
}
${pojo.getDeclarationName()} castOther = (${pojo.getDeclarationName()})other;
equalsValue = ${pojo.generateEquals("this", "castOther", jdk5)};
return equalsValue;
}
public int hashCode() {
int result = 17;
<#foreach property in pojo.getAllPropertiesIterator()> ${pojo.generateHashCode(property, "result", "this", jdk5)}
</#foreach> return result;
}
Originaly my pojo.ftl looked like this:
Code:
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}
import uk.ltd.goFurther.domain.*;
<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/>{
<#if !pojo.isInterface()><#include "PojoFields.ftl"/>
<#include "PojoConstructors.ftl"/>
<#include "PojoPropertyAccessors.ftl"/>
<#include "PojoToString.ftl"/>
<#include "PojoEqualsHashcode.ftl"/>
<#else>
<#include "PojoInterfacePropertyAccessors.ftl"/>
</#if>
<#include "PojoExtraClassCode.ftl"/>
}
</#assign>
${pojo.generateImports()}
${classbody}
But the
<#include "PojoToString.ftl"/>
<#include "PojoEqualsHashcode.ftl"/>
never got called, (because, I'm gusssing pojo.isInterface() was returning true for some reason.)
To get around this I just took the pojo.isInterface() out so pojo.ftl now looks like:
Code:
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}
import uk.ltd.goFurther.domain.*;
<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/>{
<#include "PojoFields.ftl"/>
<#include "PojoConstructors.ftl"/>
<#include "PojoPropertyAccessors.ftl"/>
<#include "PojoToString.ftl"/>
<#include "PojoEqualsHashcode.ftl"/>
<#include "PojoExtraClassCode.ftl"/>
}
</#assign>
${pojo.generateImports()}
${classbody}
I think I must be missing something...
Here is how I'm donig my revenge:
Code:
<?xml version="1.0"?>
<!--
File: $RCSfile: build.xml,v $
goFurther project tools build file responsible for generating sorce and configuation files
based on the database schema. The main target 'generateClasses' creates the following files under src/gen:
(NOTE: This code generation process is based on hibernate ANT tools - see http://www.hibernate.org/hib_docs/tools/reference/en/html_single/ )
Author: $Author: AdamGibbons $
Version: $Revision: 1.0 $
-->
<project name="goFurtherTools" basedir="../" default="hibernateCodeGen">
<property name="tools.dir" value="tools" />
<property name="src.dir" value="src" />
<property name="web.dir" value="web" />
<property name="test.src" value="test" />
<property name="lib.dir" value="${web.dir}/lib" />
<property name="build.dir" value="build" />
<property name="test.dir" value="${build.dir}/test" />
<property name="webapp.name" value="goFurther" />
<property name="gen.dir" value="gen" />
<property environment="env" />
<property name="ant.home" value="${env.ANT_HOME}" />
<property name="tomcat.home" value="${env.CATALINA_HOME}" />
<property name="tomcat.lib.dir" value="${tomcat.home}/common/lib" />
<path id="classpath">
<pathelement path="${java.class.path}/" />
<pathelement path="${build.dir}/classes" />
<pathelement path="${build.dir}/test/classes" />
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="toolslib">
<path location="lib/hibernate-tools.jar" />
<path location="lib/hibernate3.jar" />
<path location="lib/freemarker.jar" />
<path location="${jdbc.driver.jar}" />
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
<target name="hibernateCodeGen">
<copy todir="${build.dir}/classes" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/config">
<include name="*.*" />
</fileset>
</copy>
<delete failonerror="false">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain/data">
<include name="*.*" />
</fileset>
</delete>
<delete failonerror="false">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/dao/base/hibernate">
<include name="*.*" />
</fileset>
</delete>
<delete failonerror="false">
<fileset dir="${tools.dir}/hibernate/generated/config">
<include name="*.*" />
</fileset>
</delete>
<delete dir="${tools.dir}/hibernate/generated"/>
<hibernatetool
destdir="${tools.dir}/hibernate/generated"
templatepath="${tools.dir}/hibernate/templates"
>
<classpath>
<path location="${build.dir}/classes" />
<path location="${basedir}/lib" />
</classpath>
<jdbcconfiguration
packagename="uk.ltd.goFurther.domain"
propertyfile="${tools.dir}/hibernate/config/hibernate.properties"
reversestrategy="uk.ltd.goFurther.tools.hibernate.reverseEngineering.CustomReverseEngineeringStrategy"
detectmanytomany="false"
revengfile="${tools.dir}/hibernate/config/hibernate.reveng.xml"
/>
<hbm2hbmxml />
</hibernatetool>
<hibernatetool
destdir="${tools.dir}/hibernate/generated"
templatepath="${tools.dir}/hibernate/templates"
>
<classpath>
<path location="${build.dir}/classes" />
<path location="${basedir}/lib" />
</classpath>
<jdbcconfiguration
packagename="uk.ltd.goFurther.domain.data"
propertyfile="${tools.dir}/hibernate/config/hibernate.properties"
reversestrategy="uk.ltd.goFurther.tools.hibernate.reverseEngineering.CustomReverseEngineeringStrategy"
detectmanytomany="false"
revengfile="${tools.dir}/hibernate/config/hibernate.reveng.xml"
/>
<hbm2java />
</hibernatetool>
<copy todir="${tools.dir}/hibernate/generated/config" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain">
<include name="*.hbm.xml" />
</fileset>
</copy>
<delete>
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain">
<include name="*.hbm.xml" />
</fileset>
</delete>
<mkdir dir="${tools.dir}/hibernate/generated/tmp"/>
<copy todir="${tools.dir}/hibernate/generated/tmp" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain/data">
<include name="*.*" />
</fileset>
<globmapper from="*.java" to="*Data.java"/>
</copy>
<delete>
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain/data">
<include name="*.java" />
</fileset>
</delete>
<copy todir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain/data" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/tmp">
<include name="*.java" />
</fileset>
</copy>
<delete failonerror="false">
<fileset dir="${tools.dir}/hibernate/generated/tmp">
<include name="*.*" />
</fileset>
</delete>
<hibernatetool
destdir="${tools.dir}/hibernate/generated"
templatepath="${tools.dir}/hibernate/templates"
>
<classpath>
<path location="${build.dir}/classes" />
<path location="${basedir}/lib" />
</classpath>
<jdbcconfiguration
packagename="uk.ltd.goFurther.dao.base.hibernate"
propertyfile="${tools.dir}/hibernate/config/hibernate.properties"
reversestrategy="uk.ltd.goFurther.tools.hibernate.reverseEngineering.CustomReverseEngineeringStrategy"
detectmanytomany="false"
revengfile="${tools.dir}/hibernate/config/hibernate.reveng.xml"
/>
<hbm2dao />
</hibernatetool>
<copy todir="${tools.dir}/hibernate/generated/tmp" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/dao/base/hibernate">
<include name="*.*" />
</fileset>
<globmapper from="*Home.java" to="*DAOBaseHibernate.java"/>
</copy>
<delete failonerror="false">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/dao/base/hibernate">
<include name="*.*" />
</fileset>
</delete>
<copy todir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/dao/base/hibernate" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/tmp">
<include name="*.*" />
</fileset>
</copy>
<delete failonerror="false">
<fileset dir="${tools.dir}/hibernate/generated/tmp">
<include name="*.*" />
</fileset>
</delete>
<delete dir="${tools.dir}/hibernate/generated/tmp"/>
<delete failonerror="false">
<fileset dir="${src.dir}/java/uk/ltd/goFurther/dao/base/hibernate">
<include name="*.*" />
</fileset>
</delete>
<delete failonerror="false">
<fileset dir="${src.dir}/java/uk/ltd/goFurther/domain/data">
<include name="*.*" />
</fileset>
</delete>
<delete failonerror="false">
<fileset dir="${src.dir}/java/uk/ltd/goFurther/domain/conf">
<include name="*.*" />
</fileset>
</delete>
<copy todir="${src.dir}/java/uk/ltd/goFurther/dao/base/hibernate" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/dao/base/hibernate">
<include name="*.*" />
</fileset>
</copy>
<copy todir="${src.dir}/java/uk/ltd/goFurther/domain/data" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/uk/ltd/goFurther/domain/data">
<include name="*.*" />
</fileset>
</copy>
<copy todir="${src.dir}/java/uk/ltd/goFurther/domain/conf" includeemptydirs="false" overwrite="true">
<fileset dir="${tools.dir}/hibernate/generated/config">
<include name="*.*" />
</fileset>
</copy>
</target>
</project>
Here is an example of one of my Pojos:
Code:
package uk.ltd.goFurther.domain.data;
// Generated 22-May-2007 11:56:41 by Hibernate Tools 3.2.0.beta8
import uk.ltd.goFurther.domain.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* Game generated by hbm2java
*/
@SuppressWarnings("serial")
public class GameData
implements java.io.Serializable, BaseDomainObject{
private Integer id;
private int version;
private Country country;
private GameType gameType;
private String name;
private Date creationDate;
private int sizeX;
private int sizeY;
private Set knowledgeDiagrams = new HashSet(0);
private Set plays = new HashSet(0);
private Set gamePlayers = new HashSet(0);
private Set knowledgeProblemSolutions = new HashSet(0);
/** default constructor */
public GameData() {
}
/** minimal constructor */
public GameData(Country country, GameType gameType, String name, Date creationDate, int sizeX, int sizeY) {
this.country = country;
this.gameType = gameType;
this.name = name;
this.creationDate = creationDate;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
/** full constructor */
public GameData(Country country, GameType gameType, String name, Date creationDate, int sizeX, int sizeY, Set knowledgeDiagrams, Set plays, Set gamePlayers, Set knowledgeProblemSolutions) {
this.country = country;
this.gameType = gameType;
this.name = name;
this.creationDate = creationDate;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.knowledgeDiagrams = knowledgeDiagrams;
this.plays = plays;
this.gamePlayers = gamePlayers;
this.knowledgeProblemSolutions = knowledgeProblemSolutions;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public int getVersion() {
return this.version;
}
public void setVersion(int version) {
this.version = version;
}
public Country getCountry() {
return this.country;
}
public void setCountry(Country country) {
this.country = country;
}
public GameType getGameType() {
return this.gameType;
}
public void setGameType(GameType gameType) {
this.gameType = gameType;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreationDate() {
return this.creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getSizeX() {
return this.sizeX;
}
public void setSizeX(int sizeX) {
this.sizeX = sizeX;
}
public int getSizeY() {
return this.sizeY;
}
public void setSizeY(int sizeY) {
this.sizeY = sizeY;
}
public Set getKnowledgeDiagrams() {
return this.knowledgeDiagrams;
}
public void setKnowledgeDiagrams(Set knowledgeDiagrams) {
this.knowledgeDiagrams = knowledgeDiagrams;
}
public Set getPlays() {
return this.plays;
}
public void setPlays(Set plays) {
this.plays = plays;
}
public Set getGamePlayers() {
return this.gamePlayers;
}
public void setGamePlayers(Set gamePlayers) {
this.gamePlayers = gamePlayers;
}
public Set getKnowledgeProblemSolutions() {
return this.knowledgeProblemSolutions;
}
public void setKnowledgeProblemSolutions(Set knowledgeProblemSolutions) {
this.knowledgeProblemSolutions = knowledgeProblemSolutions;
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(getClass().getName()).append("@").append(Integer.toHexString(hashCode())).append(" [");
buffer.append("]");
return buffer.toString();
}
public boolean equals(Object other) {
boolean equalsValue = false;
if(this == other){
equalsValue = true;
}
if(other == null){
equalsValue = false;
}
if(!(other instanceof Game)){
equalsValue = false;
}
Game castOther = (Game)other;
equalsValue = false;
return equalsValue;
}
public int hashCode() {
int result = 17;
return result;
}
}
and the mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-May-2007 11:56:39 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="uk.ltd.goFurther.domain.Game" optimistic-lock="version" table="Game" schema="dbo" catalog="goFurther">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<version name="version" type="int">
<column name="version" not-null="true" />
</version>
<many-to-one name="country" class="uk.ltd.goFurther.domain.Country" fetch="select">
<column name="country" not-null="true" />
</many-to-one>
<many-to-one name="gameType" class="uk.ltd.goFurther.domain.GameType" fetch="select">
<column name="gameType" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="name" length="100" not-null="true" />
</property>
<property name="creationDate" type="timestamp">
<column name="creationDate" length="23" not-null="true" />
</property>
<property name="sizeX" type="int">
<column name="sizeX" not-null="true" />
</property>
<property name="sizeY" type="int">
<column name="sizeY" not-null="true" />
</property>
<set name="knowledgeDiagrams" inverse="true">
<key>
<column name="game" not-null="true" />
</key>
<one-to-many class="uk.ltd.goFurther.domain.KnowledgeDiagram" />
</set>
<set name="plays" inverse="true">
<key>
<column name="game" not-null="true" />
</key>
<one-to-many class="uk.ltd.goFurther.domain.Play" />
</set>
<set name="gamePlayers" inverse="true">
<key>
<column name="game" not-null="true" />
</key>
<one-to-many class="uk.ltd.goFurther.domain.GamePlayer" />
</set>
<set name="knowledgeProblemSolutions" inverse="true">
<key>
<column name="game" not-null="true" />
</key>
<one-to-many class="uk.ltd.goFurther.domain.KnowledgeProblemSolution" />
</set>
</class>
</hibernate-mapping>
Any help would be much appriciated !!!
Thanks in advance.
Adam.G