Reputation: 1297
I'm working on an apartment management software and I'm having an issue.
There is two entities I have :
@Entity
public class Tenant extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Apartment apartment;
}
and
@Entity
public class Apartment extends AbstractEntity { //AbstractEntity contains the id
@Column(nullable = false)
private int number;
@OneToOne
private Tenant tenant;
}
But when I do
EntityManager em = emProvider.get();
em.getTransaction().begin();
em.merge(apartment);
em.flush();
em.getTransaction().commit();
It only save the Tenant into the Apartment but I would like it also update the Apartment into the Tenant.
Do I really need to set the apartment field into the tenant or there is a way to fix it simply? Thanks
Cordially, Baskwo
Upvotes: 0
Views: 1445
Reputation: 6738
You need to declare CascadeType.ALL
in your Apartment entity. See sample
@OneToOne(cascade = CascadeType.ALL)
private Tenant tenant;
CascadeType.ALL is for all CRUD operation. Adjust CascadeType depends on your application needs.
Upvotes: 1