Java Spring jpa数据jpa组合,带有3个主键,使用IdClass

Java Spring jpa数据jpa组合,带有3个主键,使用IdClass,java,spring,hibernate,jpa,relationship,Java,Spring,Hibernate,Jpa,Relationship,我在用户和评论之间有多对多的关系。它可以工作,但用户只能发表一条评论。我需要添加另一个生成的id,使密钥唯一 评论类 @Entity @IdClass(CommentPK.class) public class Comment { @Id private Long id; @Id @ManyToOne @JoinColumn(name = "gameID" ,referencedColumnName = "id") private Game

我在用户和评论之间有多对多的关系。它可以工作,但用户只能发表一条评论。我需要添加另一个生成的id,使密钥唯一

评论类

 @Entity
@IdClass(CommentPK.class)
public class Comment {
    @Id
    private Long id;


    @Id
    @ManyToOne
    @JoinColumn(name = "gameID" ,referencedColumnName = "id")
    private Game game;

    @Id
    @ManyToOne
    @JoinColumn(name = "userID",referencedColumnName = "id")
    private User user;

    private String Text;

    public Comment() {
        super();
        this.id = null;
        this.game = null;
        this.user = null;
        Text = null;
    }
    public Comment(Game game, User user, String text) {
        this();
        this.id = null;
        this.game = game;
        this.user = user;
        Text = text;
    }
    public Comment(Game game, String text) {
        this();
        this.id = null;
        this.game = game;
        this.Text = text;
    } }//setters and getters
public class CommentPK implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long game;

    private Long user; }//setters and getters
Can not set java.lang.Long field guru.springframework.domain.CommentPK.game to guru.springframework.domain.TF_Game
评论PK

 @Entity
@IdClass(CommentPK.class)
public class Comment {
    @Id
    private Long id;


    @Id
    @ManyToOne
    @JoinColumn(name = "gameID" ,referencedColumnName = "id")
    private Game game;

    @Id
    @ManyToOne
    @JoinColumn(name = "userID",referencedColumnName = "id")
    private User user;

    private String Text;

    public Comment() {
        super();
        this.id = null;
        this.game = null;
        this.user = null;
        Text = null;
    }
    public Comment(Game game, User user, String text) {
        this();
        this.id = null;
        this.game = game;
        this.user = user;
        Text = text;
    }
    public Comment(Game game, String text) {
        this();
        this.id = null;
        this.game = game;
        this.Text = text;
    } }//setters and getters
public class CommentPK implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long game;

    private Long user; }//setters and getters
Can not set java.lang.Long field guru.springframework.domain.CommentPK.game to guru.springframework.domain.TF_Game
错误并非全部都很大

 @Entity
@IdClass(CommentPK.class)
public class Comment {
    @Id
    private Long id;


    @Id
    @ManyToOne
    @JoinColumn(name = "gameID" ,referencedColumnName = "id")
    private Game game;

    @Id
    @ManyToOne
    @JoinColumn(name = "userID",referencedColumnName = "id")
    private User user;

    private String Text;

    public Comment() {
        super();
        this.id = null;
        this.game = null;
        this.user = null;
        Text = null;
    }
    public Comment(Game game, User user, String text) {
        this();
        this.id = null;
        this.game = game;
        this.user = user;
        Text = text;
    }
    public Comment(Game game, String text) {
        this();
        this.id = null;
        this.game = game;
        this.Text = text;
    } }//setters and getters
public class CommentPK implements Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long game;

    private Long user; }//setters and getters
Can not set java.lang.Long field guru.springframework.domain.CommentPK.game to guru.springframework.domain.TF_Game

在没有生成id的情况下工作正常。

正如我在代码中看到的,错误消息指出idClass和entity之间的类型不匹配

正如您看到的,您的idclass有Long、Long、Long as类型,而您的实体有Long、Game、User。 也许可以试试下面的方法

 @Entity
 @IdClass(CommentPK.class)
 public class Comment {
@Id
private Long id;

@Id
@Column(name="gameID")
private Long gameId;

@ManyToOne
@JoinColumn(name = "gameID" ,referencedColumnName = "id", insertable=false, updatable=false)
private Game game;

@Id
@Column(name="userID")
private Long userId;

@ManyToOne
@JoinColumn(name = "userID",referencedColumnName = "id", insertable=false, updatable=false)
private User user;

private String Text;

并根据实体中的属性重命名IdClass中的字段。

为什么?你为什么把你的生活弄得这么复杂?为什么你不为你的实体使用一列,纯技术的,自动生成的主键?我需要得到游戏的评论,也可以得到用户的评论?此外,当游戏被删除时,它的评论也被删除,这并不是将用户和游戏放在实体ID中的理由。让它们成为实体的一部分,而不是它的ID。它起作用了我删除了commentPK,也从用户和游戏中删除了@ID注释,谢谢