这是我的JPA结构:
电影(看看级联类型):
@Entity
@Table(name = "movie")
public class Movie {
@Id
@Column(name = "movie_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
//@OneToMany(cascade = CascadeType.ALL, mappedBy = "primaryKey.movie") //stack overflow
@OneToMany(mappedBy = "primaryKey.movie") //works fine
private List<Rating> ratings;
....
}
评分:
@Entity
@Table(name = "rating")
@AssociationOverrides({@AssociationOverride(name = "primaryKey.movie", joinColumns = @JoinColumn(name = "movie_id")),
@AssociationOverride(name = "primaryKey.user", joinColumns = @JoinColumn(name = "imdb_user_id"))})
public class Rating {
@EmbeddedId
private RatingId primaryKey = new RatingId();
@Column(name = "rating_value")
private Integer ratingValue;
.....
}
RatingId:
@Embeddable
public class RatingId implements Serializable{
@ManyToOne
private Movie movie;
@ManyToOne
private User user;
}
当我用entityManager.merge(Movie movie)
调用CascadeType.ALL
时,我得到了StackOverflowError。如果删除级联,合并调用不会抛出错误。哪里可能有问题?
我认为这个问题与复合主键有关。 merge
在具有相同一对多关系但没有复合ID的其他实体上执行时没有错误。
答案 0 :(得分:3)
StackOverflow是由循环关系引起的。为了避免异常,我在多对多表中将密钥标记为@ManyToOne(fetch = FetchType.LAZY)
。
这就是我的表在修改后的表现:https://stackoverflow.com/a/32544519/2089491