1

基于本教程,我正在尝试使用复合键创建多对多关系最终我收到以下错误

复合键本身具有以下结构:

@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;
}
4

1 回答 1

3

在你Usrs使用useridmappedBy的应该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<>();
}
于 2021-02-05T06:29:41.680 回答