-->
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: Why foreign key column is empty when I save parent with...
PostPosted: Fri Nov 10, 2006 5:20 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 8:10 am
Posts: 28
Hello,

What annotations should I use for parentKey field in Child object so when I add Child object to Parent it will be saved with the given parent id. Like this:
Code:
Parent p = session.load(...);
Child c = new Child();
c.name = "blah blah blah";
p.children.add(c);
session.save(p);
...

When this code is executed all objects are inserted into DB but child has null parentId. :( Can anyone help me? I am confused because of composite keys. Please!

All classes and tables were simplified to improve readability! :)

I have parent and child tables:
Code:
PARENT (
  id,
  name,
  ...
)

CHILD (
  id,
  parentId,
  name,
  ...
)

Parent class looks like this (simplified version):
Code:
class Parent {
  @Id
  @GeneratedValue
  @AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "id"))
  })
  PrimaryKey key;
  ...
  @LazyCollection(LazyCollectionOption.FALSE)
  @OneToMany(cascade=javax.persistence.CascadeType.ALL, mappedBy = "parentId", targetEntity = Child.class)
  List<Child> children;
}

Child object looks like this (simplified version):
Code:
class Child {
  @Id
  @GeneratedValue
  @AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "id"))
  })
  PrimaryKey key;
  @Column(name = "name)
  String name;
  ...
  @AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "parentId"))
  })
  PrimaryKey parentKey;
}


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.