ser*_*hii 3 java stack-overflow hibernate jpa 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;
....
}
Run Code Online (Sandbox Code Playgroud)
评分:
@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;
.....
}
Run Code Online (Sandbox Code Playgroud)
评级 ID:
@Embeddable
public class RatingId implements Serializable{
@ManyToOne
private Movie movie;
@ManyToOne
private User user;
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时entityManager.merge(Movie movie),CascadeType.ALL我收到 StackOverflowError。如果删除级联,合并调用不会抛出错误。哪里可能有问题?
我认为这个问题与复合主键有关。在merge具有相同一对多关系但没有复合 id 的另一个实体上执行时没有错误。
StackOverflow 是由循环关系引起的。为了避免异常,我将多对多表中的键标记为@ManyToOne(fetch = FetchType.LAZY).
这就是我的表格在修改后的样子:https : //stackoverflow.com/a/32544519/2089491
| 归档时间: |
|
| 查看次数: |
2073 次 |
| 最近记录: |