openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mansour Al Akeel <mansour.alak...@gmail.com>
Subject Composite Primary Key with constraints
Date Thu, 30 Jan 2014 05:48:04 GMT
Hello all,

I need to create a primary key in my tables, and set constrains on
them. So I have few tables:

1- Geo
2- GeoAssociation

For the GeoAssociation, I need a primary key GeoAssociationPK. The PK
has a field, that "HAS" to be a geo_id. So here's the definition I
have for GeoAssociation:



@Embeddable
public class GeoAssociationPK implements Serializable {

    private String strId;

    private Geo geo;

    @Column(name = "str_id")
    public String getMessage() {
        return strId;
    }

    public void setMessage(String message) {
        this.strId = message;
    }

    @ManyToOne(optional = false)
    @Column(name = "geo_id")
    public Geo getGeo() {
        return geo;
    }

    public void setGeo(Geo geoFrom) {
        this.geo = geoFrom;
    }

    @Override
    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not implemented");
    }

}



This is how I define the GeoAssociation class:

@Entity
@Access(AccessType.PROPERTY)

public class GeoAssoc {

    @EmbeddedId
    private GeoAssociationPK id = new GeoAssociationPK();

    private GeoAssocType type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type")
    public GeoAssocType getType() {
        return type;
    }

    public void setType(GeoAssocType value) {
        this.type = value;
    }

    public Geo getGeoFrom() {
        return this.id.getGeo();
    }

    @Override
    public boolean equals(Object obj) {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not implemented");
    }

}


The error I am getting when building with maven enhancer is:

The id class specified by type "class entities.geo.GeoAssoc" does not
match the primary key fields of the class.  Make sure your identity
class has the same primary keys as your persistent type, including pk
field types. Mismatched property: "geo" -> [Help 1]

I don't understand the error message or how to solve it. What I need
is to be able to create and persist entity without having to
explicitly create the PK.

For example :
GeoAssociation gs = new GeoAssociation();
gs.setGeo(myGeo);
gs.setStrId(someString);


My question is how to achieve this, and resolve this error message ?


Thank you in advance.

Mime
View raw message