On May 12, 11:46 am, Rien <
rnent...@gmail.com> wrote:
> We indeed set the value to be able to identify it before it has been
> stored in the database.
Just out of curiosity, how can you tell when it's "new"?
> When a value is stated as a GeneratedValue I would expect Ebean not to
> try to insert it even when it's set.
If Ebean did not try to insert the PK value, I would think it should
also fail to save a new object instance if the ID is already set. I
believe JPA will fail, though I'm too lazy to verify it.
On the issue of identity (really aside from the PK insert issue) you
are setting the ID presumably because your hashcode/equals methods
refer to it. I'm only "reasonable" confident my implementations are
appropriate/complete. I'm not really sure what happens when I put a
new (null id) object in a map, for example. Would you mind sharing
your hashcode/equals implementation? I believe mine is fairly close to
the Ebean enhanced version:
PersistentEntity.java (superclass for all my entities):
@Override
public int hashCode() {
return getId() == null ? 0 : getId().hashCode();
}
@Override
public boolean equals(Object object) {
if (object == null) return false;
if (object == this) return true; // useful when both objects are new
and we are doing something like list.contains(entity)
if (object.getClass().equals(this.getClass())) { // must be same
subclass of PersistentEntity
Object id1 = ((PersistentEntity) object).getId();
Object id2 = getId();
if ((id1 == null) || (id2 == null)) return false;
else return id1.equals(id2);
} else {
return false;
}
}
Thanks.
/Daryl