JPA。级联合并上的Stackoverflow

问题描述 投票:1回答:1

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

java hibernate jpa stack-overflow cascade
1个回答
3
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.