-->
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: Sets and contains - equals and hashcode
PostPosted: Tue Nov 21, 2006 1:48 pm 
Newbie

Joined: Tue Nov 21, 2006 1:32 pm
Posts: 7
Hi

I have a persistent object "Model", which contains a set of persistent objects "Feature". "Feature" has a single parameter, "name". In "Feature" I have overridden equals and hashcode to check the "name" value instead. e.g.

Code:
public int hashCode() {
  return this.getName().toUpperCase().hashCode();
}

public boolean equals( Object _obj ) {
  if ( _obj instanceof Feature ) {
    return ( (Feature) _obj ).getName().toUpperCase().equals( this.getName().toUpperCase() );
  } else if ( _obj instanceof String ) {
    return ( (String) _obj ).toUpperCase().equals( this.getName().toUpperCase() );
  }
  return false;      
}



I want to be able to do a comparison based on a String. e.g.
Code:
Model model;
...
boolean contains = model.getFeatures().contains( "FEATURE_NAME" );


however, this always returns false. Everything I have read about overriding equals and hashcode in hibernate talks about comparing Objects across sessions, whereas I want to compare Objects based on their contents. Is this possible?

I have a solution by putting the Set into an ArrayList, but I had to override ArrayList to reverse the equals check. The default is that contains(String myString) does myString.equals(thisElement) which always failed, instead of thisElement.equals( myString ) which uses my overridden equals and hashCode. This is what I had to reverse by overriding indexOf in the ArrayList, which is EXTREMELY messy.

I'm sure that there must be a better way to do this, but I can't figure it out.

Please help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 9:28 am 
Newbie

Joined: Tue Nov 21, 2006 1:32 pm
Posts: 7
bump?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 12:58 pm 
Newbie

Joined: Tue Nov 21, 2006 1:32 pm
Posts: 7
bump. again.


Top
 Profile  
 
 Post subject: Define your key as the upper case result of your name
PostPosted: Thu Nov 23, 2006 8:35 pm 
Newbie

Joined: Mon Nov 20, 2006 5:16 pm
Posts: 7
Location: PARIS
Hibernate will set equals and hashcode based on the key of your object.

It overrides your equals and hashcode for sets.

So you'll have:

YourClass{
private String mKey;
private String mName;

public void setName(String pName) {
mName = pName;
mKey = mName.toUppercase();
}

etc.

}

Of course, you'll have to set your mapping correctly ...

a++ Cédric

ps: BTW looks more like you need to get familiar with enums and how to handle them in Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 5:33 am 
Newbie

Joined: Tue Nov 21, 2006 1:32 pm
Posts: 7
Hi crouvrais

thanks for the input, and please forgive me if I'm being a noob with this.

I assume by key you mean the primary key? In my case this is a long, not the string column I want to check on - we use numerical foreign-key references. Please can you give an example of
Quote:
you'll have to set your mapping correctly ...


Here are my mappings:
Models
Code:
<hibernate-mapping package="model">
   <class name="Model" table="MODELS">
      <cache usage="read-only" />
      <id column="PK_MODEL_ID"
         name="id"
         type="long" >
         <generator class="native">
            <param name="sequence">MODELS_PK_SEQ</param>
         </generator>
      </id>
...
...
      <set name="features" table="MODEL_FEATURES" inverse="false">
         <key column="FK_MODEL_ID" />
         <many-to-many class="Feature" column="FK_FEATURE_ID" />
      </set>



and Features:
Code:
<hibernate-mapping package="model">
   <class name="Feature" table="FEATURES">
      <cache usage="read-only" />
      <id column="PK_FEATURE_ID"
         name="id"
         type="long" >
         <generator class="native">
            <param name="sequence">FEATURES_PK_SEQ</param>
         </generator>
      </id>
...
...
      <property name="name"
         column="FEATURE_NAME"
         type="string"
         not-null="true" />


forgive me if I am missing something blindingly obvious. I have looked through the documentation, and can't see anything.

Also, we are still using java 2 (1.4) so we don't use enums.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 9:09 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Why don't you do this : -

Code:
boolean contains = model.getFeatures().contains( new Feature("FEATURE_NAME") );


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 9:35 am 
Newbie

Joined: Tue Nov 21, 2006 1:32 pm
Posts: 7
thanks JayeshJ, that works. I thought of that, but I assumed that it would try and create a new db entry if it didn't exist. Just run a test, and it doesn't seem to, so that solution works. Should have tried it before :p


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 10:32 am 
Newbie

Joined: Tue Dec 13, 2005 7:20 am
Posts: 2
Location: Dunfermline, Scotland
When you create a new object, it only creates a new row in the db if you call save(). So you can quite happily create in-memory only instances of your persistent classes without affecting the db.


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.