foreign key and ID generated as a composite primary key in JPA

Viewed 35

Trying to persist a instance of Class, I got the error Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field model.entity.Class.Class.classID to org.hibernate.id.IdentifierGeneratorHelper$2

I know there is the Embeddable and EmbeddableId annotations, but I want to know if is possible to use IdClass to make this table relationship. Can someone help me what is wrong?

Edit: I was trying to persist the object new Class(em.getReference(SystemUser.class, "test@hotmail.com"), "subjectTest");

@Entity
@Table(name = "Class")
@IdClass(value = model.entity.Class.Class.class)
public class Class
{
    @Id
    @JoinColumn(name = "classUserlogin", referencedColumnName = "userLogin")
    @Transient
    private SystemUser SystemUser;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long classID;
    @Column(name = "subject", nullable = false, length = 100)
    private String subject;
   
    ...
}
@Entity
@Table(name = "SystemUser")
public class SystemUser
{
    @Id
    @Column(name = "userLogin", nullable = false, length = 40)
    private String userLogin;

    ...
}

Database:

CREATE TABLE SystemUser {
    PRIMARY KEY(userLogin)
}

CREATE TABLE Class {
    PRIMARY KEY(classUserlogin, classID)
    FOREIGN KEY(classUserlogin) REFERENCES SystemUser(userLogin)
}
0 Answers
Related