-->
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.  [ 4 posts ] 
Author Message
 Post subject: persisting parent with child
PostPosted: Tue Sep 11, 2007 9:18 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
I have two objects (noticia and noticia_lang). noticia_lang is a collection used by noticia.

I am trying to create a new noticia and add two noticia_lang objects to noticia. When i persist noticia i get an error saying that id_noticia cannot be null. Mening that the noticia_lang foreign key is not being generated for me??

Here are my beans with annotations:

public class Noticia implements Serializable {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "image", nullable = false)
private String image;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "idNoticia")
private Collection<NoticiaLang> noticiaLangCollection= new ArrayList();
--------------------------

public class NoticiaLang implements Serializable {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "title", nullable = false)
private String title;

@Column(name = "description", nullable = false)
private String description;

@JoinColumn(name = "codigo_language", referencedColumnName = "code")
@ManyToOne
private Language codigoLanguage;

@JoinColumn(name = "id_noticia", referencedColumnName = "id")
@ManyToOne
private Noticia idNoticia;
---------------------------

here is my code:

EntityManager emMySQL = HibernateUtil.getEntityManagerFactory().createEntityManager();


emMySQL.getTransaction().begin();

Noticia noticia = new Noticia();
noticia.setImage("test");

NoticiaLang language = new NoticiaLang("test1", "testdesc");
language.setCodigoLanguage(new Language("es","lalalalal"));
NoticiaLang language2 = new NoticiaLang("test1", "testdesc");
language2.setCodigoLanguage(new Language("es","lalalalal"));
noticia.getNoticiaLangCollection().add(language);
noticia.getNoticiaLangCollection().add(language2);

emMySQL.persist(noticia);

emMySQL.getTransaction().commit();
emMySQL.close();

any ideas??


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 10:12 am 
Newbie

Joined: Tue Sep 11, 2007 9:05 am
Posts: 13
Ok i got it. I just read the part about bidirectional relation and owners.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 10:17 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I think it might be becuase NoticiaLang is the owning side of the relationship, and as such it is the only entity that can update the relationship in the DB.

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 11, 2007 4:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
stefanoskollias wrote:
Ok i got it. I just read the part about bidirectional relation and owners.

Yes that was the problem

_________________
Emmanuel


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

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.