Change of entity to reflect on related entities

0

I have two entities, for example:

Obs: Code fiction 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;
}

When changing the attribute of the Cell 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 chamada object to notice the change in celular ?

    
asked by anonymous 03.09.2014 / 23:15

1 answer

1

Try doing entityManager.refresh (mobile);

  

Refresh the state of the instance from the database, overwriting changes made to the entity, if any.

    
04.09.2014 / 01:10