JPA。级联合并上的Stackoverflow

时间:2015-09-22 22:16:13

标签: java hibernate jpa stack-overflow cascade

这是我的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的其他实体上执行时没有错误。

1 个答案:

答案 0 :(得分:3)

StackOverflow是由循环关系引起的。为了避免异常,我在多对多表中将密钥标记为@ManyToOne(fetch = FetchType.LAZY)

这就是我的表在修改后的表现:https://stackoverflow.com/a/32544519/2089491