user1622058
user1622058

Reputation: 483

Hibernate collections are NULL after persist

JPA entity Batch has OneToMany relation to Event:

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

and Event has ManyToOne relation to Batch:

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

after creating new instance and persisting it the containingEvents are still NULL. But when I use empty Set:

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

then after persisting the containingEvents are replaced with Hibernate's PersistentSet. I would expect this replacement to happen even in first case. Why is it not happening?

DAOs are implemented by Spring Data JPA.

Spring Boot 2.0.4.RELEASE
Spring Data JPA 2.0.4.RELEASE
Hibernate 5.2.17.Final
Hibernate JPA 2.1 1.0.2.Final

Upvotes: 1

Views: 678

Answers (1)

Artem Namednev
Artem Namednev

Reputation: 417

You need add CascadeType in your @OneToMany annotation, for example:

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

And you Event class must contain:

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

Upvotes: 1

Related Questions