Joined: Thu Sep 02, 2010 11:17 am Posts: 1
|
I have the code below that don't to do correctly transitive persistence.
//begin entity PessoaAnotated @Entity @Table (name="TB_PESSOA") public class PessoaAnotated {
@Id @GeneratedValue (strategy=GenerationType.IDENTITY) private Long id; @OneToMany (cascade=CascadeType.ALL,targetEntity=VeiculoAnotated.class) private Collection<VeiculoAnotated> veiculos = new ArrayList<VeiculoAnotated>(); //end entity PessoaAnotated
//begin entity VeiculoAnotated @Entity @Table(name="TB_VEICULO_ANOTATED") public class VeiculoAnotated { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id;
@ManyToOne (optional=false) private PessoaAnotated proprietario; //end entity VeiculoAnotated
//begin code PessoaAnotated pessoa = new PessoaAnotated();
pessoa.setNome("teste hibernate"); pessoa.setIdade(30); VeiculoAnotated veiculo = new VeiculoAnotated(); pessoa.getVeiculos().add(veiculo); 1 veiculo.setProprietario(pessoa); 2 HibernateUtil.currentEntityManager().persist(pessoa); //end code
Why I need to set both veiculo in pessoa (mark "1") and "pessoa in veiculo (mark "2")" and ? On book Java Persistence with Hibernate page 269 tells that is not necessary!
Does anyone know why this behavior occurs?
|
|