-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: reveng - toString, hashCode, equals
PostPosted: Tue May 22, 2007 7:05 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
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

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 8:37 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Max or Ananasi, any ideas? >.>
Anyone else? ._.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 9:49 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
Also a few other things I'd like to know if i can do, thought I would just tack them on here instead of making a new post...
If we take the GameData object I've already posted above it has the following:
Code:
     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);

All of the Sets listed at the end hibernate has added for me, basicaly these are reverse looks and can come in handy. However if you set these objects you tend to run into problems. I would like to remove all the setter methods for those objects with revenge, is that posible?
Also I'd like to remove the setter for Id and version for simalar reasons.
I'm guessing you could do something like that with a CustomRevengeStratergy, but how?

As always, any help would be much appriciated!

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 4:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
there is no default tostring/equals impl (for good reasons!) so if you want them you need to add <meta attribute="use-in-equals">true</meta> in reveng.xml for the properties/columns you want to enable e.g.e quals for.

with respect to the get/setters then that is not a problem of reverse engineering but for the code generation so it is currently something that you would control via the *.ftl templates.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:44 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
How can I easily add <meta attribute="use-in-equals">true</meta> to all Columns in all Tables?
My reveng.xml just looks like this lol:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
   <table-filter match-catalog="goFurther" match-schema="dbo" match-name=".*"/>
</hibernate-reverse-engineering>

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 11:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
just add the meta to the class....but I have no idea why you would want to do that considering most models have reqursive/bidirectional associations

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.