Domanda

I have implemented a simple entity ejb with a @version annotation. I expect that the version number will increase after each update of an entity.

@Version
public Integer getVersion() {
    return version;
}
public void setVersion(Integer version) {
    this.version = version;
}

But this seems not to work as expected. Also each time when I read an entity, the version number increases automatically(!?). I expect that the version only increases after a commit?

Can anybody explain why my version increases also on reads?

È stato utile?

Soluzione

Finally I found the reason for the strange behavior. The problem occurs during a method where I copy all values from my (still attached) entity into a detached domain model object. This all works well, since I read an attached entity contaning a complexe data structure (a vector containg HashMaps). I copied this values with the .addAll method from the List interface:

List activePropertyValue = (List)mapEntry.getValue();
// value contains HashMaps!
List detachePropertyValue = new Vector();
detachePropertyValue.addAll(activePropertyValue);

But it seems that this changed the hash value from the attached entity data property. So after all the entity was updated in the database, and the version number increases.

I solved the problem by detaching the entity before I copy all values:

manager.detach(aEntity);
.....
List activePropertyValue = (List)mapEntry.getValue();
// value contains HashMaps!
List detachePropertyValue = new Vector();
detachePropertyValue.addAll(activePropertyValue);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top