Hi, I am having a problem related to many-to-one mappings. I have three classes in a situation that Preco has a many-to-one relationship with Custo, which has a set of Preco and also a many-to-one to Tipo which also has a set of Custo. Everything is working fine, I can retrieve the values from the classes and save each of them individually without problems.
What I am trying to do is something like this (all the instances of Preco, Custo and Tipo exist)
Preco preco = session.load(Preco.class, precoId);
Custo custo = preco.getCusto();
//=> trying to change the Tipo from Custo
custo.getTipo.setId(new Integer(tipoId));
session.save(preco);
I think you can get the picture now, what I am trying to do is to get the existent Custo from Preco and change the existent Tipo from Custo, and persist this. I am having no success on doing that, I get no errors, and if I change any properties of Custo (not many-to-one, simple properties) they are persisted as well, but the many-to-one property change is not persisted. If I do like this, however, it works:
Preco preco = session.load(Preco.class, precoId);
Custo custo = preco.getCusto();
Tipo tipo = new Tipo(new Integer(tipoId));
custo.setTipo(tipo);
session.save(preco);
Is this an expected behavior? Should it work the other way?
Thanks a lot.
|