openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luis Fernando Planella Gonzalez <lfpg....@gmail.com>
Subject Problem using generated id in equals() / hashCode() / toString()
Date Mon, 01 Mar 2010 11:53:28 GMT
Hi.
I'm using OpenJPA 1.2.2 with OpenEJB 3.1.2, and facing an odd problem...
I have a BaseEntity class, which all other entities extend, with the following definition:

@MappedSuperclass
public abstract class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Override
    public boolean equals(Object obj) {
        if (!getClass().isInstance(obj)) {
            return false;
        }
        BaseEntity other = (BaseEntity) obj;
        return id != null && id.equals(other.id);
    }

    public Long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        return id == null ? System.identityHashCode(this) : id.hashCode();
    }

    @Override
    public final String toString() {
        return ClassHelper.getUnqualifiedName(getClass()) + "#" + id;
    }
}


When trying to persist a graph of objects, during flush, an StoreException is thrown, having
as nested exceptions 3 times the following (probably 1 per instance persisted): 
<openjpa-1.2.2-r422266:898935 nonfatal user error> org.apache.openjpa.util.InvalidStateException:
The generated value processing detected an existing value assigned to this field: package.BaseEntity.id.
 This existing value was either provided via an initializer or by calling the setter method.
 You either need to remove the @GeneratedValue annotation or modify the code to remove the
initializer processing.

However, I'm not assigning a value by hand: I've debugged and what's there is actually the
value generated by the DB identity...

The intriguing is that if I remove the id from equals(), toString() and hashCode(), no error
is thrown. But if I do that, I can no longer trust HashMaps / HashSets.

Is there a solution for it?

Thanks in advance.

--
Luis Fernando Planella Gonzalez

Mime
View raw message