基于本教程,我尝试使用复合键创建多对多关系最后我得到以下错误
组合键本身具有以下结构:
@Embeddable
@Data
public class MovieRatingsKey implements Serializable {
@Column(name = "movieid")
private Movies movieid;
@Column(name = "userid")
private Usrs userid;
}
类电影:
@Entity
@Data
public class Movies {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer movieid;
/*not related to the question*/
@OneToMany(mappedBy = "movieid", fetch = FetchType.LAZY)
private Set<Ratings> rates = new HashSet<>();
}
用户:
@Entity
@Data
public class Usrs {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer userid;
@OneToMany(mappedBy = "userid", fetch = FetchType.LAZY)
private Set<Ratings> rates = new HashSet<>();
}
关联表:
@Entity
@Data
public class Ratings {
@EmbeddedId
private MovieRatingsKey id;
/*not related to the question*/
@ManyToOne
@MapsId("movieid")
@JoinColumn(name = "movieid")
Movies movie;
@ManyToOne
@MapsId("userid")
@JoinColumn(name = "userid")
Usrs user;
}
在您的Usrs
中,您使用的userid
在mappedBy
中应该是user
,如下所示。
@Entity
@Data
public class Usrs {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer userid;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Set<Ratings> rates = new HashSet<>();
}