0
I have two entities, for example:
Note: Fictional code to facilitate understanding of the problem.
@Entity
public class Celular{
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
private String modelo;
@OneToMany(mappedBy = "celular")
private List<chamada> chamadas;
}
@Entity
public class Chamada{
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "idcelular")
private Celular celular;
}
It happens that when changing the attribute of the Cellular object, it is not reflected in the Call object. Example:
...
Chamada ch=celular.getChamada().get(0);
System.out.println(ch.getCelular().getModelo()); //imprime "NOKIA"
celular.setModelo("Motorola");
//persistir
getEntityManager().getTransaction().begin();
celular = getEntityManager().merge(entity);
getEntityManager().getTransaction().commit();
getEntityManager().close();
Chamada ch=celular.getChamada().get(0);
System.out.println(celular.getModelo()); //imprime "Motorola"
System.out.println(ch.getCelular().getModelo()); //imprime "Nokia"
What would be the correct procedure for the object chamada
notice the change in celular
?
Your problem should be time to save the relationship. You should always do a.setB(b) and b.set(A). Be sure to perform the relationships correctly before persisting in DB.
– uaiHebert