I'm having problems when saving entities in my DB.
I have something like (very simplified) :
@Entity
public class Building {
@OneToMany(mappedBy = "building", fetch = FetchType.EAGER)
private List<Employee> employees;
}
@Entity
public class Employee {
@NotNull
@ManyToOne
@JoinFetch(INNER)
@JoinColumn
private Building building;
@PostLoad
private void onLoad(){
if (this.plannedOrder == null) {
//For old entities in this DB, update plannedOrder if null
if (this.order < FIRST.getCode()) {
this.plannedOrder = FIRST;
} else if (this.order >= FIRST.getCode() && this.order < SECOND.getCode()) {
this.plannedOrder = SECOND;
} else if (this.order >= DEFAULT.getCode() && this.order < SEC_LAST.getCode()) {
this.plannedOrder = DEFAULT;
} else if (this.order >= SEC_LAST.getCode() && this.order < LAST.getCode()) {
this.plannedOrder = SEC_LAST;
} else if (this.order >= LAST.getCode()) {
this.plannedOrder = LAST;
} else {
this.plannedOrder = DEFAULT;
}
}
}
The problem is when I save a Employee entity. In the Building entity you will have all employees modified because the @PostLoad annotation, so JPA will try to update these entities. However, I only want to update the Employee entity.
Is some way to do this without changing the relations in my model? Could be a solution to remove the onLoad function?
Thanks!
EDITED:
Added the PostLoad code of PostLoad. This is a fix executed for old entities in DB without this field to be updated when saved.
EDITED 2
And this is how I'm saving my entity Employee with my EntityManager (if is a new Object, persist, else update it)
if (entity.getId() == null) {
entityManager.persist(entity);
return entity;
}
return entityManager.merge(entity);
Employee
toBuilding
or the otherEmployee
s of theBuilding
. Show have you "save" theEmployee
. – PieriaEmployee
s? Remember that any changes to retrieved objects within a transaction will be persisted, without the need to call themerge
-method. Also, when you say that employees are "modified", what does this mean? – Pieria