JPA。 Stackoverflow级联合并 [英] JPA. Stackoverflow on cascade merge
问题描述
这是我的JPA结构:
电影(查看级联类型):
Movie (look at cascade types):
@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。如果删除级联,合并调用不会抛出错误。哪里可能有问题?
When I call entityManager.merge(Movie movie)
with CascadeType.ALL
I get the StackOverflowError. If remove cascading, merge call doesn't throw the error. Where may be a problem?
堆栈跟踪:
http://paste.ofcode.org/YHMTG5QesmgWdfFAA6RAaZ
我认为这个问题与复合主键有关。 merge
在具有相同一对多关系但没有复合id的另一个实体上执行时没有错误。
I think this problem related to composite primary key. There is no error when merge
performed on another entities with the same one-to-many relationship, but without composite id.
推荐答案
StackOverflow是由循环关系造成的。为避免出现异常,我在多对多表中标记为 @ManyToOne(fetch = FetchType.LAZY)
。
StackOverflow was caused by cyclic relations. To avoid exception I marked keys in many-to-many table as @ManyToOne(fetch = FetchType.LAZY)
.
这就是我的表格在修改之后的样子: https://stackoverflow.com/a/32544519/2089491
That's how my tables look after modifications: https://stackoverflow.com/a/32544519/2089491
这篇关于JPA。 Stackoverflow级联合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!