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.  [ 5 posts ] 
Author Message
 Post subject: Cascading in bidirectional relationships
PostPosted: Tue Apr 04, 2006 8:04 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:28 pm
Posts: 26
I have a relationship that looks something like this:

Code:
class Parent
{
    @OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
    Set<Child> children;

    @OneToOne
    @JoinColumn(nullable=false)
    Child defaultChild;

    public void Parent()
    {
        defaultChild = new Child();
        this.children.add(defaultChild);
    }
}

class Child
{
    @ManyToOne
    @JoinColumn(nullable=false)
    Parent parent;
}


I'm finding that no matter what cascade options I set, I still get a "org.hibernate.PropertyValueException: not-null property references a null or transient value: blah.Child.parent" exception.

Is there some way to make it work? There don't appear to be any examples exactly like this in the documentation. I want, when Parent is created, for there always to be one instance of Child created along with it.

Thanks in advance,
Jeff


Top
 Profile  
 
 Post subject: Show your hbm configuration file.
PostPosted: Tue Apr 04, 2006 10:00 am 
Newbie

Joined: Tue Apr 04, 2006 9:56 am
Posts: 10
I recently worked on this. I was able to save parent child.
Please post your hbm configuration file. and also you need to have inverse=true in the parent cfg.


Top
 Profile  
 
 Post subject: Re: Show your hbm configuration file.
PostPosted: Tue Apr 04, 2006 3:58 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:28 pm
Posts: 26
dhalliyur wrote:
I recently worked on this. I was able to save parent child.
Please post your hbm configuration file. and also you need to have inverse=true in the parent cfg.


There isn't a hbm config file - this is using the EJB3 system under JBoss. My example in this topic pretty much included all of the relevant content. Note that mappedBy is the equivalent of inverse="true".

However, if you want to see the real deal, here is the Parent object:

http://subetha.tigris.org/source/browse ... cvs-markup

Here is the child object:

http://subetha.tigris.org/source/browse ... cvs-markup

There are three relationships:

ManyToOne from Role to MailingList
OneToMany from MailingList to Role (being mappedBy, this is the equivalent of "inverse")
OneToOne from MailingList to Role (the default role)

Both the ManyToOne and OneToOne relationships are nullable=false.

The constructor of MailingList (the parent) must create, and include for persistence, a single default role.

Thanks,
Jeff Schnitzer


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 6:53 pm 
Newbie

Joined: Sat Feb 11, 2006 1:31 pm
Posts: 3
Location: New York, NY
I think if you change
Code:
defaultChild = new Child();
        this.children.add(defaultChild);

to
Code:
defaultChild = new Child();
        defaultChild.parent = this;
        this.children.add(defaultChild);


it will work. In the majority of cases, both sides of a bi-directional association should be set explicitly.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 10:07 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:28 pm
Posts: 26
serdagger wrote:
I think if you change
Code:
defaultChild = new Child();
        this.children.add(defaultChild);

to
Code:
defaultChild = new Child();
        defaultChild.parent = this;
        this.children.add(defaultChild);


it will work. In the majority of cases, both sides of a bi-directional association should be set explicitly.


My mistake when creating the example - yes, in fact, I am setting both sides of the bidirectional association. You can see it in the actual code linked above. Still produces an exception.

I think the extra OneToOne relationship is confusing hibernate in some way. I'll have to narrow down a test case.

Thanks,
Jeff Schnitzer


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