[英]Spring jpa data jpa composite with 3 primary key using IdClass
我與用戶和評論之間有很多關系。 它有效,但是用戶只能發表評論。 我需要添加另一個使密鑰唯一的生成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
評論PK
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與實體之間的類型不匹配。
如您所見,您的idclass具有Long,Long,Long作為類型,而您的實體具有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中的字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.