-->
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.  [ 5 posts ] 
Author Message
 Post subject: Question with hbm2java and composite ids with a userType
PostPosted: Tue Aug 09, 2005 4:51 pm 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
I want to map a table with a composite ID and it just so happens that one of the fields in that composite ID is an enumeration, so I'm using a userType (called Roles) to reference that field. I read that for composite IDs, it's best to create a composite identifier class, which I've done (it's called RolesCompositeId). I'm also using hbm2java to generate mapping sources and when I run it, I find that hbm2java overwrites the RolesCompositeId.java class I built by hand. I'm worried about this because I read that composite classes should override equals and hashCode, and the hbm2java generated class does not contain these methods.

Am I OK to go with the hbm2java generated version of my composite identifier class?


Hibernate version: version 3.0.5, 25 May 2005

Mapping documents: Mapping for composite id"

Code:
<hibernate-mapping>

    <class name = "UserRoles" table="dash_roles">
 
        <composite-id name="rolesCompositeId" class="RolesCompositeId">
           <key-property name="userId" column="user_id"/>
           <key-property name="roles"  type="Roles" column="role"/>
        </composite-id>
       
    </class>     
       
</hibernate-mapping>



Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 10:08 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Hi,

i am using the latest cvs-version ob hibernatetools.
It generates the id-classes together with their
equals and hashcode-methods like:

Code:
   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
       if ( (other == null ) ) return false;
       if ( !(other instanceof CapflergId) ) return false;
       CapflergId castOther = ( CapflergId ) other;
         
       return (this.getNrMandant()==castOther.getNrMandant())
&& (this.getDatStichtag()==castOther.getDatStichtag()) || ( this.getDatStichtag()!=null && castOther.getDatStichtag()!=null && this.getDatStichtag().equals(castOther.getDatStichtag()) )
&& (this.getSlProdukt()==castOther.getSlProdukt())
&& (this.getNrGeschaeftken()==castOther.getNrGeschaeftken()) || ( this.getNrGeschaeftken()!=null && castOther.getNrGeschaeftken()!=null && this.getNrGeschaeftken().equals(castOther.getNrGeschaeftken()) )
&& (this.getIdErgebnis()==castOther.getIdErgebnis())
&& (this.getIdCapFloorlet()==castOther.getIdCapFloorlet());
   }
   
   public int hashCode() {
         int result = 17;
         
         result = 37 * result + (int) this.getNrMandant();
         result = 37 * result + this.getDatStichtag().hashCode();
         result = 37 * result + (int) this.getSlProdukt();
         result = 37 * result + this.getNrGeschaeftken().hashCode();
         result = 37 * result + (int) this.getIdErgebnis();
         result = 37 * result + (int) this.getIdCapFloorlet();
         return result;
   }   


I am not sure about the hibernate-tools version 3.0 alpha 4a.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 10:55 am 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
Interesting.

I just re-ran my build script and I'm def. not getting hashCode and equals:

Code:
public class RolesCompositeId  implements java.io.Serializable {

    // Fields   

     private Integer userId;
     private Roles roles;


    // Constructors

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

    // Property accessors

    /**
     *
     */
    public Integer getUserId() {
        return this.userId;
    }
   
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    /**
     *
     */
    public Roles getRoles() {
        return this.roles;
    }
   
    public void setRoles(Roles roles) {
        this.roles = roles;
    }
}


Are you calling hbm2java the same way as me?

Code:
    <target name="hbm2java" depends="build-user-types" description = "Generate Java source from the O/R mapping files">
   
       <taskdef name         = "hibernatetool"
                classname    = "org.hibernate.tool.ant.HibernateToolTask"
                classpathref = "master-classpath"/>
             
       <hibernatetool destdir="${src.dir}">
          <configuration configurationfile="hibernate.cfg.xml" />
          <hbm2java/>
       </hibernatetool>
       
    </target>


I guess the most recent CVS snapshot has functionality to generate those methods. I'm not even sure how to pull that rev. from the Hiberante downloads area.

It probably doesn't matter anyway as I'm going to change my table definition to contain a unique id. Just seems easier to work with Hibernate that way.

Thanks so much for you note.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 3:01 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Hi,

my ant-script just looks the same.

BTW, accessing the hibernate-cvs-repository from
behind a firewall can be quite a pain.
Here is what I did:

cvs -d:pserver;proxy=192.168.6.100;proxyport=80:anonymous@cvs-pserver.sourceforge.net:443/cv
sroot/hibernate login
Logging in to :pserver:anonymous@cvs-pserver.sourceforge.net:443:/cvsroot/hibernate
CVS password:

and:

cvs -z3 -d:pserver;proxy=192.168.6.100;proxyport=80:anonymous@cvs-pserver.sourceforge.net:44
3/cvsroot/hibernate co -P HibernateExt

192.168.6.100 is our proxy.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 11, 2005 3:03 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
And I forgot:
working with an unique id is just the best solution!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.