{ @GeneratedValue(str" /> { @GeneratedValue(str" /> { @GeneratedValue(str"/>

Problem with inserting objects into database that have composite ids

20 views Asked by At
@Data
@Entity
@Table(name = "comment")
@IdClass(CommentEntityPK.class)
public class CommentEntity implements BaseEntity<Integer> {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "idcomment", nullable = false)
    private Integer idcomment;
    @Basic
    @Column(name = "Text", nullable = false, length = 200)
    private String text;
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "idUser", nullable = false)
    private Integer iduser;
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "idprogram", nullable = false)
    private Integer idprogram;
    @ManyToOne
    @JoinColumn(name = "iduser", referencedColumnName = "iduser", nullable = false)
    private UserEntity userByIdUser;
    @ManyToOne
    @JoinColumn(name = "idprogram", referencedColumnName = "idprogram", nullable = false)
    private ProgramEntity programByIdProgram;
}

This is my CommentEntity class that has been mapped by hibernate, but it also generated it's primary key class, CommentEntityPK:

@Data
public class CommentEntityPK implements Serializable {
    @Column(name = "idcomment", nullable = false)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer idcomment;
    @Column(name = "iduser", nullable = false)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer iduser;
    @Column(name = "idprogram", nullable = false)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer idprogram;
}

now i have a CommentRequest and Comment class which are used to insert and retrieve comments from the database:

@Data
public class CommentRequest {
    private String text;
    private Integer iduser;
    private Integer idprogram;
    private UserEntity userByIdUser;
    private ProgramEntity programByIdProgram;
}
@Data
public class Comment {
    private Integer idcomment;
    private String text;
    private Integer iduser;
    private Integer idprogram;
    private Integer userByIdUser;
    private Integer programByIdProgram;
}

Now the issue occurs when i try to add a new comment to the database i get this error:

2024-03-30T10:35:34.325+01:00 ERROR 7056 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.orm.jpa.JpaSystemException: Identity generation isn't supported for composite ids] with root cause

org.hibernate.id.IdentifierGenerationException: Identity generation isn't supported for composite ids

can someone explain to me what im doing wrong, and how do i insert these types of objects(objects that have their primary keys mapped in a separate class)

When i send these JSON objects:

{
    "text": "Text",
    "idUser": 1,
    "idProgram": 1,
    "userByIdUser": {
     "iduser": 1
    },
    "programByIdProgram": {
        "idprogram": 1
    }
}

and ive also tried sending a JSON object like:

{
    "text":"toppp",
    "iduser":"1",
    "idprogram":"1",
    "userByIdUser":"1",
    "programByIdProgram":1
}
0

There are 0 answers