-->
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: JPA - Foreign Id not automatically assigned after insertion
PostPosted: Thu Nov 12, 2009 5:18 pm 
Newbie

Joined: Sat Oct 17, 2009 9:50 am
Posts: 4
Hi There

I am currently struggling that the foreign Id (parent Id) is not inserted at the DB table of the child object. I think it has something to do with that the id of the parents id is automatically generated by the DB (autoincrement). So what do I wrong?

Let me explain the details of my setup. I am using Hibernate 3.3 with JPA and MySQL 5.1
I have a simple One-To-One bidirectional - Relationship.

@Entity
@Table (name="main")
public class Main {

@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="id")
private int id;

@OneToOne (mappedBy="main", cascade=CascadeType.ALL)
private Child child;
}

@Entity
@Table (name="child")
public class Child {

@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="id")
private int id;

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="main_id", unique=true, insertable=true, updatable=true)
private Main main;

}

// at Java Runtime...
Main m = new Main();
Child c = new Child();
m.setChild(c);
// do only a save on the main, child automatically saved too
entityManager.persist(m);

ISSUE:
==> If I now look at the table entries, there has been a child and a main persisted, the child and also the main got also their own primary id assigned, but the "main_id" of the child is empty ?

Did I miss something?

Thank you in advance regards
Mark


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.