Aymen Kanzari
Aymen Kanzari

Reputation: 2013

Entity with @EmbeddedId , @GeneratedValue and @ManyToOne

I worked with postgres database, I have a Conventionnement entity with ManyToOne relationship with convention and organization, I want to create 3 primary keys convention_id, organization_id and the GeneratedValue id, i used @Embeddable like the below example, but I had the below error

Caused by: org.hibernate.annotations.common.AssertionFailure: Declaring class is not found in the inheritance state hierarchy: ConventionnementIdentity

and when I moved the id in Conventionnement class i had this error

ConventionnementIdentity must not have @Id properties when used as an @EmbeddedId

@Entity
@Table(name = "conventionnement")
public class Conventionnement implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ConventionnementIdentity conventionnementIdentity;

    @MapsId("convention_id")
    @ManyToOne
    private Convention convention;

    @MapsId("organization_id")
    @ManyToOne
    private Organization organization;

    //getter and setter
}

@Embeddable
public class ConventionnementIdentity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @Column(name = "convention_id", insertable = false, updatable = false)
    private Long conventionId;

    @Column(name = "organization_id", insertable = false, updatable = false)
    private Long organizationId;

    //getter and setter
}

Upvotes: 3

Views: 12494

Answers (1)

Matteo Baldi
Matteo Baldi

Reputation: 5828

You have defined an @EmbeddedId, but inside it you declare another @Id, thus the error. Make the generated id point to the correct id column in your table (below I've assumed "id" as column name):

@Embeddable
public class ConventionnementIdentity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator", sequenceName="YOUR_DB_SEQ", allocationSize=1)
    private Long id;

    @Column(name = "convention_id", insertable = false, updatable = false)
    private Long conventionId;

    @Column(name = "organization_id", insertable = false, updatable = false)
    private Long organizationId;

    //getter and setter
}

Upvotes: 3

Related Questions