-->
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: OneToOne relation is null when loaded from proxy collection
PostPosted: Thu Feb 21, 2008 9:47 am 
Newbie

Joined: Thu Dec 14, 2006 7:36 am
Posts: 6
I have classic parent-child relationship of objects of same type. The object has additional OneToOne eager relation with another entity. When an child object is loaded with parent.getChildren(), the OneToOne relation is null. However if I make HQL query mimicking the parent.getChildren() call, the OneToOne is properly initialized. The parent-child objects are on the inverse side of the OneToOne relation.

Do you have any ideas why loading an object as part of proxy collection differs from similar HQL query?

The generated SQL query to database properly joins with OneToOne table and returns not null columns for the OneToOne entity. When I enabled hibernate debug mode, I see that the OneToOne entity is instantiated but the relation is created only from the one of the sides - on the non-inverse one, the other side remains null.

Any ideas? Thank you in advance!


Top
 Profile  
 
 Post subject: Re: OneToOne relation is null when loaded from proxy collect
PostPosted: Thu Feb 21, 2008 2:46 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
What are the mappings like?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 4:44 am 
Newbie

Joined: Thu Dec 14, 2006 7:36 am
Posts: 6
Here are mappings:

Code:
public class TreeIdentity {
...........
    @ManyToOne()
    @JoinColumn(name="parent_id")
    public TreeIdentity getParent() {
        return parent;
    }
...........
    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name="parent_id")
    @IndexColumn(name="child_position")
    public List<TreeIdentity> getChildren() {
        return children;
    }
...........
    @OneToOne(
        mappedBy="revisionObject"
        cascade={CascadeType.ALL}
    )
    public Revision getLastRevision() {
        return lastRevision;
    }
}

public class Revision {
..........
    @OneToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name="identity_revision_id")
    public TreeIdentity getRevisionObject() {
        return revisionObject;
    }
..........
}


This mapping creates foreign key "identity_revision_id" in Revision table pointing to TreeIdentity. When I call getChildren() on TreeIdentity, the getLastRevision() is null.

I changed the inverse side (e.g. I put the join column in TreeIdentity table and put "mappedBy" in the Revision and it worked like a charm.

It is bugging me what was the real cause of the problem


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 1:32 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I can't say why it was not working for you. I did an example with the same mapping and it loads the relations for me. So, mapping wise you are good. There might be differences in HB version or .....


Farzad-


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.