users@glassfish.java.net

Re: RE: UISelectMany and POJOs: "Value Is Not Valid"

From: <glassfish_at_javadesktop.org>
Date: Tue, 17 Jul 2007 22:31:01 PDT

> Here is my Users.equals():
>
> public boolean equals(Object object) {
> if (!(object instanceof Users)) {
> return false;
> }
> Users other = (Users)object;
> if (this.username != other.username && (this.username
> == null ||
> !this.username.equals(other.username))) return false;
> return true;

Is username the @Id field for your Users class?

The important rule for entities is that they can only use fields annotated with @Id (or the embedded varients) in equals and hashcode calculation.

It is in fact essential that they use those @Id fields or you will have strange persistence errors sneak up and eat you from behind!

I have considered using refection to write a EntityUnitTester class that will automatically test your equals and hashcode implementations and poke random data into all the other fields to check that you are not using them for equality.

The reason why it is the @Id field is that these fields are the ones that correspond to instance "identity". The annotation means if these fields are the same, you have the same entity instance (it might have newer field values that you are instructing the container to pos back to the database)

> -----Original Message-----
> That is known, I usually override the 'equals'
> method,
>
> For instance, with entities, I usually do the
> comparation with the ID of the
> entity such as:
>
> class MyEntity {
> ...
> private long id;
>
> @Id
> public long getId() {
> return id;
> ...
>
> @Override
> public boolean equals(Object obj) {
> if (obj instanceof MyEntity) {
> MyEntity entity = (MyEntity) obj;
> return this.id== entity.getId();
> } else {
> return false;
> }
> }
>
[Message sent by forum member 'stephenconnolly' (stephenconnolly)]

http://forums.java.net/jive/thread.jspa?messageID=227049