-->
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.  [ 2 posts ] 
Author Message
 Post subject: Issue with persisting child objects: Child has composite PK
PostPosted: Fri May 23, 2008 2:20 pm 
Newbie

Joined: Tue Mar 27, 2007 2:00 pm
Posts: 4
I am working on an application that uses EJB3 and JPA + Seam. In one of the screens in the application, I am creating new instances of a Parent object at run-time and also, creating new instances of Child objects. Following are the definitions (and mappings) of the two classes:

@Entity
public class Parent
{
@OneToMany(fetch=FetchType.LAZY, mappedBy="parent",
cascade={CascadeType.ALL}
private List<Child> children;

public void addChild(Child child)
{
this.children.add(child);
child.setParent(this);
}

public void removeChild (Child c)
{
this.children.remove(c);
c.setParent(null);
}

..
}

Here is the definition of the Child class (notice that Child has composite PK).

@Entity
@IdClass(ChildPK.class)
public clss Child
{
@Id
private Long key1;

@Id
private Long key2;

@Id
@Column(name="PARENT_ID")
private Long parentId;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PARENT_ID", nullable=false, insertable=false, updatable=false)
private Parent parent;

...
}


The problem is when I hit the Submit button on the screen, I merge the detached Parent and its associated Children with the EJB tier and then eventually call flush(). In this case, on the flush() Hibernate runs the INSERT on the Parent object first and then, runs INSERT INTO CHILD object next. It fails throwing an ORA error saying: "Cannot insert NULL into "CHILD.PARENT_ID" column when executing the INSERT INTO CHILD statement.

In the GUI, when I create new Child objects, I call addChild() method on the Parent to store the children created. The addChild() method and removeChild() methods are really relationship management methods to manage this Parent-Child relationship.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 23, 2008 3:02 pm 
Newbie

Joined: Wed May 14, 2008 3:45 pm
Posts: 8
I am not sure about this approach of the primary key generation.

I tried with @EmbeddedId approach for primarykey generation.

You can try modifing the setter for parent as follows


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

if(parent != null){
this.parentId = parent.getId();
}

}


As I followed embedded Id approach I used the following


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

if(this.id == null){
this.id = new ChildPk();
}

if(parent != null){
this.id.setParentId(parent.getId());
}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.