Hi all !
I have a relation 1-to-n between Constitution and Information. In Constitution I have a set of Information objects.
I think that the method "contains()" for the set does not work properly. If I used an iterator instead the object was found in the collection. When I have used contains() instead it returned false. I do not why. Below you have my configuration and an unit test.
Please help me !!
Thanks in advance
Hibernate version:3.0.5
Domain classes:
Code:
// Information
....
public static Information generate(Constitution constitution,
InfoType infoType)
{
Information info = new Information();
info.constitution = constitution;
constitution.addInformation(info);
// .....
}
public boolean equals(Object other)
{
if (this == other) {
return true;
}
if (!(other instanceof Information)) {
return false;
}
final Information o = (Information) other;
if (id != null && !id.equals(o.id)) {
return false;
}
if (id == null && !(hashCode() == o.hashCode())) {
return false;
}
return true;
}
public int hashCode()
{
if (id == null) {
return super.hashCode();
} else {
return id.hashCode();
}
}
public Constitution getConstitution()
{
return constitution;
}
protected void setConstitution(Constitution constitution)
{
this.constitution = constitution;
}
....
// Constitution
....
public static Constitution generate()
{
Constitution constitution = new Constitution();
constitution.informations = new LinkedHashSet();
return constitution;
}
public boolean equals(Object other)
{
if (this == other) {
return true;
}
if (!(other instanceof Constitution)) {
return false;
}
final Constitution o = (Constitution) other;
if (id != null && !id.equals(o.id)) {
return false;
}
if (id == null && !(hashCode() == o.hashCode())) {
return false;
}
return true;
}
public int hashCode()
{
if (id == null) {
return super.hashCode();
} else {
return id.hashCode();
}
}
public Set getInformations()
{
return informations;
}
protected void setInformations(Set informations)
{
this.informations = informations;
}
public void addInformation(Information information)
{
information.setPosn(new Integer(this.informations.size()));
this.informations.add(information);
information.setConstitution(this);
}
....
Mapping documents:Code:
<!-- Constitution.hbm.xml-->
.....
<set
name="informations"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
order-by="posn"
>
<key
column="constitution_id"
>
</key>
<one-to-many
class="test.Information"
/>
</set>
....
<!-- Information -->
...
<many-to-one
name="constitution"
class="test.Constitution"
cascade="none"
outer-join="auto"
column="constitution_id"
not-null="true"
/>
<property
name="posn"
type="java.lang.Integer"
column="posn"
/>
....
Code between sessionFactory.openSession() and session.close():Code:
/// I have the following unit test
Constitution c = Constitution.generate();
InfoType type = InfoType.generate(typeName);
service.saveInfoType(type);
service.saveConstitution(c);
Information info1 = Information.generate(c, type);
Information info2 = Information.generate(c, type);
service.saveConstitution(c);
System.out.println("-------------------------111");
Integer pos1 = info1.getPosn();
Integer pos2 = info2.getPosn();
System.out.println("-------------------------222");
// ++++ Here we will get an error. I do not why !!!!. ++++
//+++++ If I iterate the collection "info1" is found !!! ++++
//++++ Seems that contains is not working properly +++++
assertTrue("1. information set must contain " + info1 +
", but contains:" +
c.getInformations(), c.getInformations().contains(info1));
System.out.println("-------------------------333");
Full stack trace of any exception that occurs:
1. information set must contain (20,0), but contains:[(20,0), (21,1)]
junit.framework.AssertionFailedError: 1. information set must contain (20,0), but contains:[(20,0), (21,1)] at test.ConstitutionServiceTest.testSwapInformationPosition(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Name and version of the database: MySQL 4.1 InnoDB.
The generated SQL (show_sql=true):
Hibernate: insert into info_type (name) values (?)
Hibernate: insert into constitution values ( )
Hibernate: insert into information (constitution_id, posn, info_type_id) values (?, ?, ?)
Hibernate: insert into information (constitution_id, posn, info_type_id) values (?, ?, ?)
-------------------------111
-------------------------222