Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 739
Aymen Kanzari Avatar asked Nov 25 '25 20:11

Aymen Kanzari


1 Answers

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
}
like image 160
Matteo Baldi Avatar answered Nov 27 '25 08:11

Matteo Baldi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!