-->
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: Parent / Child with Composite Child Keys
PostPosted: Thu Sep 02, 2010 3:12 pm 
Beginner
Beginner

Joined: Fri Nov 13, 2009 4:05 pm
Posts: 30
I have a parent entity object that contains three lists of child records. Each of the three lists are represented by their own entity object class with the primary key for these child entities being a composite key that contains the parent's unique identity primary key.

Parent
ParentId - Identity/Unique Sequence Column, managed by Hibernate

ChildEntityA
ParentId
RecordType

ChildEntityB
ParentId
oldStatus
newStatus
type

In my code what I want to be capable of doing would be something like:
Code:
  Parent p = new Parent();
  ChildEntityA a = new ChildEntityA();
  ChildEntityB b = new ChildEntityB();
  p.addChildA(a);
  p.addChildB(b);
  getEntityManager().persist(p);


The problem I experience is that the parent's id isn't being set in the child entity objects at the time the records are being persisted. I have seen several articles on this on various web sites and tried them all and yet nothing seems to work.

Here is a brief snap shot of the relationship in the parent class:
Code:
  @OneToMany(mappedBy="parent", cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
  public List<ChildEntityA> getChildA() {
    return childA;
  }
  public void setChildA(List<ChildEntityA> childA) {
    this.childA = childA;
  }
  @OneToMany(mappedBy="parent", cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
  public List<ChildEntityB> getChildB() {
    return childB;
  }
  public void setChildB(List<ChildEntityB> childB) {
    this.childB = childB;
  }


Child A looks like:
Code:
@Entity
@Table(name="childa")
public class ChildEntityA
{
  @Embeddable
  public static class Pk implements Serializable {
    @Column(name="parent_id",nullable=false,updatable=false)
    private Long parentId;
    @Column(name="record_type",nullable=false,updatable=false,length=25)
    private String recordType;
    // getter and setters
  }

  @EmbeddedId
  private Pk id = new Pk();
  @Column(name="parent_name",length=25,nullable=false)
  private String name;
  @ManyToOne
  private Parent parent;

  public Parent getParent() {
    return parent;
  }
  public void setParent(Parent parent) {
    this.parent = parent;
  }

  // getter and setters
}


Can anyone explain what I could be doing incorrectly?


Top
 Profile  
 
 Post subject: Re: Parent / Child with Composite Child Keys
PostPosted: Fri Sep 03, 2010 11:14 am 
Beginner
Beginner

Joined: Fri Nov 13, 2009 4:05 pm
Posts: 30
Any thoughts?


Top
 Profile  
 
 Post subject: Re: Parent / Child with Composite Child Keys
PostPosted: Fri Nov 19, 2010 5:34 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

Even I am facing the same problem. Is your issue resolved? If yes, can you let me know the resolution please.

Regards,
Harish


Top
 Profile  
 
 Post subject: Re: Parent / Child with Composite Child Keys
PostPosted: Wed Sep 21, 2011 6:44 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

This has been resolved. The solution is.. we need to populate the parent object variable in the child class too in order to carry forward the primary key to the child class

Cheers,
Harish


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.