-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: @OneToMany malfunction by only one side
PostPosted: Thu Sep 02, 2010 11:37 am 
Newbie

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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.