持久化后,Hibernate集合为NULL

时间:2018-09-18 21:00:16

标签: java spring hibernate spring-boot jpa

JPA实体BatchOneToMany具有Event关系:

@OneToMany(mappedBy = "owningBatch")
private Set<Event> containingEvents;

EventBatch具有ManyToOne关系:

@ManyToOne
@JoinColumn(name = "BATCH_ID")
private Batch owningBatch;

创建新实例并将其持久化后,containingEvents仍为NULL。 但是当我使用空的Set时:

@OneToMany(mappedBy = "owningBatch")
private Set<Event> containingEvents = Sets.newHashSet();

然后在持久化containingEvents之后,将其替换为Hibernate的PersistentSet。 我希望即使在第一种情况下也会发生这种替换。为什么没有发生?

DAO由Spring Data JPA实现。

Spring Boot 2.0.4.RELEASE
Spring Data JPA 2.0.4.RELEASE
休眠5.2.17.Final
Hibernate JPA 2.1 1.0.2。最终版

1 个答案:

答案 0 :(得分:1)

您需要在CascadeType批注中添加@OneToMany,例如:

@OneToMany(mappedBy = "owningBatch", cascade = CascadeType.ALL)
private Set<Event> containingEvents;

您的Event类必须包含:

@ManyToOne
@JoinColumn(name = "batch_id", referencedColumnName = "id")
private Batch owningBatch;