JPA实体Batch
与OneToMany
具有Event
关系:
@OneToMany(mappedBy = "owningBatch")
private Set<Event> containingEvents;
和Event
与Batch
具有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。最终版
答案 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;