-->
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.  [ 7 posts ] 
Author Message
 Post subject: Set and HashCode
PostPosted: Wed Aug 17, 2005 3:31 pm 
Newbie

Joined: Wed Oct 06, 2004 4:39 pm
Posts: 17
Hi,

I have the classes:

UnidadMedida and ConversionUnidadMedida

public class UnidadMedida {

private Long id = null;
private int version;
private String nombre;
private Set convertibleA;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

public String getNombre() {
return nombre;
}

public void setNombre(String nombre) {
this.nombre = nombre;
}

public Set getConvertibleA() {
return convertibleA;
}

public void setConvertibleA(Set convertibleA) {
this.convertibleA = convertibleA;
}

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

public int hashCode() {
return new HashCodeBuilder().append(this.getNombre()).toHashCode();
}

}

public class ConversionUnidadMedida {

private Long id = null;
private int version;
private UnidadMedida deUnidadMedida;
private UnidadMedida aUnidadMedida;
private float factorConversion;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

public UnidadMedida getDeUnidadMedida() {
return deUnidadMedida;
}

public void setDeUnidadMedida(UnidadMedida deUnidadMedida) {
this.deUnidadMedida = deUnidadMedida;
}

public UnidadMedida getAUnidadMedida() {
return aUnidadMedida;
}

public void setAUnidadMedida(UnidadMedida aUnidadMedida) {
this.aUnidadMedida = aUnidadMedida;
}

public float getFactorConversion() {
return factorConversion;
}

public void setFactorConversion(float factorConversion) {
this.factorConversion = factorConversion;
}

public boolean equals(Object other) {
if (!(other instanceof ConversionUnidadMedida)) {
return false;
}
ConversionUnidadMedida castOther = (ConversionUnidadMedida) other;
return new EqualsBuilder().append(this.getDeUnidadMedida(),
castOther.getDeUnidadMedida()).append(this.getAUnidadMedida(),
castOther.getAUnidadMedida()).isEquals();
}

public int hashCode() {
return new HashCodeBuilder().append(this.getDeUnidadMedida()).append(
this.getAUnidadMedida()).toHashCode();
}

}

Mapping documents:
<hibernate-mapping>
<class
name="ar.gov.ceride.inventorymgt.domain.UnidadMedida"
table="unidad_medida"
dynamic-update="false"
dynamic-insert="false"
lazy="true"
>

<id
name="id"
type="long"
>
<column
name="id"
/>
<generator class="native">
</generator>
</id>

<version
name="version"
type="int"
column="version"
access="property"
unsaved-value="undefined"
/>

<property
name="nombre"
type="string"
update="true"
insert="true"
access="property"
>
<column
name="nombre"
length="20"
not-null="true"
unique="true"
/>
</property>

<set
name="convertibleA"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
order-by="id_aunidadmedida asc"
>

<key
column="id_deunidadmedida"
>
</key>

<one-to-many
class="ar.gov.ceride.inventorymgt.domain.ConversionUnidadMedida"
/>
</set>

</class>

</hibernate-mapping>

<hibernate-mapping>
<class
name="ar.gov.ceride.inventorymgt.domain.ConversionUnidadMedida"
table="conversion_unidadmedida"
dynamic-update="false"
dynamic-insert="false"
lazy="true"
>

<id
name="id"
type="long"
>
<column
name="id"
/>
<generator class="native">
</generator>
</id>

<version
name="version"
type="int"
column="version"
access="property"
unsaved-value="undefined"
/>

<many-to-one
name="deUnidadMedida"
class="ar.gov.ceride.inventorymgt.domain.UnidadMedida"
cascade="none"
outer-join="true"
update="true"
insert="true"
access="property"
foreign-key="fk_conversion_de_unidadmedida"
>
<column
name="id_deunidadmedida"
unique-key="unique_conversionunidadmedida"
not-null="true"
/>
</many-to-one>

<many-to-one
name="AUnidadMedida"
class="ar.gov.ceride.inventorymgt.domain.UnidadMedida"
cascade="none"
outer-join="true"
update="true"
insert="true"
access="property"
foreign-key="fk_conversion_a_unidadmedida"
>
<column
name="id_aunidadmedida"
unique-key="unique_conversionunidadmedida"
not-null="true"
/>
</many-to-one>

<property
name="factorConversion"
type="float"
update="true"
insert="true"
access="property"
>
<column
name="factor"
not-null="true"
/>
</property>

</class>

</hibernate-mapping>

Hibernate version:
2.1

My problem is when retrieve instances of UnidadMedida in the server and use them in the client as instances detached: the hashCode() in ConversionUnidadMedida use the properties 'deUnidadMedida' and 'AUnidadMedida', but when the set is created, the property 'deUnidadMedida' is null. Then this property is initialized. The problem occur when i want remove a instance of the set, the remove() return false, because el hashcode of the instance is not equal to the hashcode used when the set was generated. Is it not ok use the property 'deUnidadMedida' to generate the hashcode?

thanks.


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:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 3:37 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
You shouldn't use IDs to create the hashcode. If it is possible, use only business keys.
Read this topic for more information:
http://www.hibernate.org/109.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 3:44 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Can you initialize it before add to the set? You can configure the mapping to get it initialized.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 3:53 pm 
Newbie

Joined: Wed Oct 06, 2004 4:39 pm
Posts: 17
Hi,

I donĀ“t use ID's in the hashcode, I use the property 'nombre' of UnidadMedida.
To retrieve i use:
Criteria criteria = (Criteria) getHibernateOperations().getCriteria(
UnidadMedida.class);
// Inicializa coleccion de conversion de unidades de medida
getHibernateOperations().setFectchMode(criteria, "convertibleA",
FetchMode.EAGER);
// Recupera
Set unidadesMedida = new LinkedHashSet(getHibernateOperations().find(
criteria));

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:13 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Set the lazy atribute of many-to-one to false.

<many-to-one
name="deUnidadMedida"
class="ar.gov.ceride.inventorymgt.domain.UnidadMedida"
cascade="none"
outer-join="true"
update="true"
insert="true"
access="property"
foreign-key="fk_conversion_de_unidadmedida"
lazy="false"
>

But I don't know if this works in hibernate 2.1. Here, in hibernate 3.0, this works well. Look my code for a many-to-one:

<many-to-one name="porticoDuplo" column="PORTICO_DUPLO_ID" lazy="false"/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:24 pm 
Newbie

Joined: Wed Oct 06, 2004 4:39 pm
Posts: 17
In Hibernate 2 the tag 'lazy' is not aplicable to many-to-one, i use outer-join="true"

thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:56 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Are you using the lastest version of hibernate? (2.1.8) ?
There is no problem in using 'deUnidadMedida' to generate the hashcode. The problem is that the deUnidadMedida is null. But I think that you are doing the right thing, because you are using FetchMode.EAGER.


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