Search code examples
jpamappingeclipselink

JPA: There should be one non-read-only mapping defined for the primary key field


I'm getting a An internal error occurred accessing the primary key object [class domein.PersoonSessieKey]. Internal Exception: java.lang.NoSuchMethodException: domein.PersoonSessieKey.getPersoon()error while trying to run my application. I've had a look at similar questions but I can't figure out what I'm doing wrong.

I've tried using Embeddable, but since I had no idea what I was doing, this was also throwing an error, which made me try ClassId. However now I'm getting an error as well and I have no idea what's causing it.

Entity Class

@Entity
@Access(AccessType.PROPERTY)
@IdClass(PersoonSessieKey.class)
@Table(name = "PersoonSessie")
public class ClubPersoonSessie implements Serializable {

    private ClubSessie sessie;
    private ClubPersoon persoon;

    @Id
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "SessieId")
    public ClubSessie getSessie() {
        return sessie;
    }

    public void setSessie(ClubSessie sessie) {
        this.sessie = sessie;
    }

    @Id
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "PersoonId")
    public ClubPersoon getPersoon() {
        return persoon;
    }

    public void setPersoon(ClubPersoon persoon) {
        this.persoon = persoon;
    }

    public ClubPersoonSessie() {
    }
}

Primary Key class

public class PersoonSessieKey implements Serializable {

    private int persoonId;
    private int sessieId;

    public int getPersoonId() {
        return persoonId;
    }

    public int getSessieId() {
        return sessieId;
    }

    public PersoonSessieKey(int persoonId, int sessieId) {
        this.persoonId = persoonId;
        this.sessieId = sessieId;
    }

    public PersoonSessieKey() {
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 89 * hash + this.persoonId;
        hash = 89 * hash + this.sessieId;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PersoonSessieKey other = (PersoonSessieKey) obj;
        if (this.persoonId != other.persoonId) {
            return false;
        }
        if (this.sessieId != other.sessieId) {
            return false;
        }
        return true;
    }

}

Solution

  • The properties of the IdClass should match the fields annotated with @Id in your entity. This is clearly not the case in the code you posted.

    domein.PersoonSessieKey.getPersoon() is expected (and not found) as you have a persoon property annotated withId

    To make it work with the idclass approach

    • Add same fields as in idclass to the enity and annotate them with @Id
    • Remove @Id from relations
    • Mark relations as not updatable and not insertable (to address the issue from the post title, that is multiple writable fields mapped to the same column)