-->
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.  [ 8 posts ] 
Author Message
 Post subject: equals() and hashcode() methods not generated using hbm2java
PostPosted: Wed Aug 17, 2005 8:19 am 
Newbie

Joined: Wed Aug 10, 2005 8:55 am
Posts: 2
Location: India
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Hi,

My hibernate mapping file, Track.hbm.xml is given below:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="com.oreilly.hh.Track" table="TRACK">
      <meta attribute="class-description">
         Represents a single playable track in the music database.
         @author Vara Prasad Bokka (with help from Hibernate)
      </meta>
      
      <id name="id" type="int" column="TRACK_ID">
         <meta attribute="scope-set">protected</meta>
         <generator class="assigned"/>
      </id>
      
      <property name="title" type="string" not-null="true"/>
      
      <property name="filePath" type="string" not-null="true"/>
      
      <property name="playTime" type="time">
         <meta attribute="field-description">Playing time</meta>
      </property>
      
      <property name="added" type="date">
         <meta attribute="field-description">When the track was created</meta>
      </property>
      
      <property name="volume" type="short" not-null="true">
         <meta attribute="field-description">How loud to play the track</meta>
      </property>
      
   </class>
</hibernate-mapping>


My build.xml is given below:

Code:
<?xml version="1.0"?>
<project name="Harnessing Hibernate: The Developer's Notebook" default="db" basedir=".">
   
   <!-- Set up properties containing important project directories -->
   <property name="source.root" value="src"/>
   <property name="class.root" value="classes"/>
   <property name="lib.dir" value="lib"/>
   <property name="data.dir" value="data"/>
   
   <!-- Set up the class path for compilation and execution -->
   <path id="project.class.path">
      <!-- Include our own classes, of course -->
      <pathelement location="${class.root}" />
      <!-- Include jars in the project library directory -->
      <fileset dir="${lib.dir}">
         <include name="*.jar"/>
      </fileset>   
   </path>
   
   <target name="db" description="Runs HSQLDB database management UI
      against the database file--use when application is not running">
      
      <java classname="org.hsqldb.util.DatabaseManager" fork="yes">
         <classpath refid="project.class.path"/>
         <arg value="-driver"/>
         <arg value="org.hsqldb.jdbcDriver"/>
         <arg value="-url"/>
         <arg value="jdbc:hsqldb:${data.dir}/music"/>
         <arg value="-user"/>
         <arg value="sa"/>
      </java>
   </target>
   
   <!-- Teach Ant how to use Hibernate's code generation tool -->
   <taskdef name="hbm2java"
          classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
          classpathref="project.class.path"/>
   
   <!-- Generate the java code for all mapping files in our source tree -->
   <target name="codegen" description="Generate Java source from the O/R mapping files">
      <hbm2java output="${source.root}">
         <fileset dir="${source.root}">
            <include name="**/*.hbm.xml"/>
         </fileset>
      </hbm2java>
   </target>
</project>


When I run the build file, it generates the Track.java file as given below:

Code:
package com.oreilly.hh;

import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
*          Represents a single playable track in the music database.
*          @author Vara Prasad Bokka (with help from Hibernate)
*       
*/
public class Track implements Serializable {

    /** identifier field */
    private Integer id;

    /** persistent field */
    private String title;

    /** persistent field */
    private String filePath;

    /** nullable persistent field */
    private Date playTime;

    /** nullable persistent field */
    private Date added;

    /** persistent field */
    private short volume;

    /** full constructor */
    public Track(Integer id, String title, String filePath, Date playTime, Date added, short volume) {
        this.id = id;
        this.title = title;
        this.filePath = filePath;
        this.playTime = playTime;
        this.added = added;
        this.volume = volume;
    }

    /** default constructor */
    public Track() {
    }

    /** minimal constructor */
    public Track(Integer id, String title, String filePath, short volume) {
        this.id = id;
        this.title = title;
        this.filePath = filePath;
        this.volume = volume;
    }

    public Integer getId() {
        return this.id;
    }

    protected void setId(Integer id) {
        this.id = id;
    }

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

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

    public String getFilePath() {
        return this.filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    /**
     * Playing time
     */
    public Date getPlayTime() {
        return this.playTime;
    }

    public void setPlayTime(Date playTime) {
        this.playTime = playTime;
    }

    /**
     * When the track was created
     */
    public Date getAdded() {
        return this.added;
    }

    public void setAdded(Date added) {
        this.added = added;
    }

    /**
     * How loud to play the track
     */
    public short getVolume() {
        return this.volume;
    }

    public void setVolume(short volume) {
        this.volume = volume;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("id", getId())
            .toString();
    }

}


Now the problem is that it doesn't generate the following methods:

Code:
public boolean equals(Object other) {
  if(!(other instanceof Track)) return false;
  Track castOther = (Track) other;
  return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();
   
}


Code:
public int hashCode() {
  return new HashCodeBuilder().append(getId()).toHashCode();
}


And also it gives the following error message:

import org.apache.commons.lang.builder.ToStringBuilder;

The import org.apache.commons cannot be resolved

What might be the problem?
Please help?

Regards,
Vara Prasad Bokka,

_________________
Vara Prasad Bokka


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 8:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
#1 you dont have any meta attribute=use-in-equals in your mapping
#2 to use the commons builders you need to have commons-lang in your classpath

(#2 is not needed in h3 tools since we dont generate code that depends on commons-lang anymore)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: about code genration using alpha 5
PostPosted: Thu Aug 25, 2005 3:06 pm 
Newbie

Joined: Thu Aug 25, 2005 1:27 pm
Posts: 4
Hi

I´m using the ant task with the jars that come in alph5 and put in my hbm the <meta attribute="use-in-equals"/> with no results, here is the mapping file and the build.xml task.

Regards Rodney

<?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 auto-import="true">

<class name="cu.desoft.dds.domain.Person" table="personas">
<id name="id" column="per_id" type="long" unsaved-value="null">
<meta attribute="use-in-equals"/>
<generator class="increment"/>
</id>
<property name="name" column="per_nombre" type="string" length="100" not-null="true"/>
<property name="address" column="per_direccion" type="string" length="255" not-null="true"/>
<property name="age" column="per_edad" type="long" not-null="true"/>
<property name="sexo" column="per_sexo" type="string" length="1" not-null="true"/>
<many-to-one name="country" class="cu.desoft.dds.domain.Country" column="per_pai_id"/>
</class>

<class name="cu.desoft.dds.domain.Country" table="paises">
<id name="id" column="pai_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" column="pai_nombre" type="string" length="100" not-null="true"/>
</class>

</hibernate-mapping>

**********************************************
build.xml

<target name="gen-classes-3" >
<hibernatetool>
<configuration >
<fileset id="hibernate.mapping.files" dir="${src.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java destdir="src_temp"/>
</hibernatetool>
</target>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmm - and .java file does not have an equals method ?

what happens if you add the use-in-equals to a <property>

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ah sorry - now what is wrong

it should be <meta attribute="use-in-equals">true</meta>

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:37 pm 
Newbie

Joined: Thu Aug 25, 2005 1:27 pm
Posts: 4
well

in .java file there is no equals method, i put the meta attribute in a property and nothing happens to

<property name="name" column="per_nombre" type="string" length="100" not-null="true">
<meta attribute="use-in-equals"/>
</property>

Regards Rodney


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:51 pm 
Newbie

Joined: Thu Aug 25, 2005 1:27 pm
Posts: 4
ok, let me test


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 3:56 pm 
Newbie

Joined: Thu Aug 25, 2005 1:27 pm
Posts: 4
now works great thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.