Hello all,
I have a parent entity with a set of "Files"(super-class). Using discriminator values I have generated another set of domain objects. For the first time when I execute the program I can add/remove the elements from the set. But when I do it for the second time it is not reflecting the database. However the strange thing is that the same set of code is executed in both the cases. I tried printing the size of set for first execution and second execution. First time after the objects are removed from the Set the size of set reduces. But for the second time the size of Set remains the same even if remove() is called over its iterator.
Please suggest me some solution.
Mapping in main XML
<set name="Files" table="METADATA_FILES" inverse="true" lazy="true" cascade="all">
<key column="TUBE_ID"/>
<one-to-many class="com.imc.sts.domain.Files"/>
</set
Mapping in Files XML
<discriminator column="FILETYPE_ID" type="string" />
<subclass name="com.imc.sts.domain.InsertFiles" lazy="false" discriminator-value="1">
</subclass>
<subclass name="com.imc.sts.domain.OtherFiles" lazy="false" discriminator-value="2">
</subclass>
<subclass name="com.imc.sts.domain.GenotypeSeqFiles" lazy="false" discriminator-value="3">
</subclass>
<subclass name="com.imc.sts.domain.VectorFiles" lazy="false" discriminator-value="4">
</subclass>
<subclass name="com.imc.sts.domain.InsertVectorFiles" lazy="false" discriminator-value="5">
</subclass>
Overloaded equals() method in sub-classes :
public boolean equals(Object other) {
if ( !(other instanceof GenotypeSeqFiles) ) return false;
GenotypeSeqFiles castOther = (GenotypeSeqFiles) other;
Object[] selfArray = new Object[] {this.getFilePath()};
Object[] otherArray = new Object[] {castOther.getFilePath()};
return new EqualsBuilder()
.append(selfArray, otherArray)
.isEquals();
}
Code :
// Clear the old files
Set filesSet = tube.getFiles();
if(filesSet == null) {
filesSet = new HashSet();
} else if(filesSet.size() > 0) {
Iterator i = filesSet.iterator();
// Clear the old Files in filesSet
while(i.hasNext()) {
Object obj = i.next();
if(obj instanceof GenotypeSeqFiles || obj instanceof InsertFiles || obj instanceof VectorFiles
|| obj instanceof InsertVectorFiles || obj instanceof OtherFiles){
i.remove();
}
}
}
// Add New files
for(int counter=0; counter < genotypeSeqFilesArray.length; counter++){
GenotypeSeqFiles file = new GenotypeSeqFiles();
file.setParentTube(tube);
file.setFilePath(genotypeSeqFilesArray[counter][0]);
filesSet.add(file);
}
// Set the new files set in the tube object.
tube.setFiles(filesSet);
// Set the domain object in Tube object
tube.setGenotypeSequence(genotypeSequence);
Regards,
Nitin
|