-->
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.  [ 6 posts ] 
Author Message
 Post subject: Embedded object not loading when some fields are NULL
PostPosted: Mon Nov 01, 2010 1:02 pm 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
Using Hibernate 3.5.6 Final. I have a one-to-one containment relationship between Object and ObjectRetirement. ObjectRetirement has a FK to the containing Object and two other non-key, nullable fields (Version and Reason). I just converted the ObjectRetirement class from an Entity to an @Embeddable.

When saving the containing Object, the ObjectRetirement table gets properly populated. When calling get() on the Object, the ObjectRetirement instance is properly retrieved and populated IF at least one of the non-key fields are populated in the DB. But if I have persisted the ObjectRetirement with (objectIdKey, null, null), the ObjectRetirement instance is set to NULL on the Object when retrieved.

It's as if Hibernate's logic is, "well, the embedded object exists in the DB and has its FK populated but the other fields are NULL so I'm just going to set the entire ObjectRetirment object NULL".

When I was declaring ObjectRetirement as an Entity, I was able to get() it even when both fields were null. Why does Embedding the class change this? Is there an annotation to have Hibernate populate the object even though those nullable fields are null?

Here's the mapping from Object:
Code:
    @Embedded
    @AttributeOverrides({@AttributeOverride(name = "version", column = @Column(name = "versionid", table = "ObjectRetirement")),
            @AttributeOverride(name = "reason", column = @Column(name = "reasonid", table = "ObjectRetirement")) })
    public ObjectRetirementDO getObjectRetirement() {
        return _objectRetirement;
    }


Here's the ObjectRetirement Class highlights:
Code:
@Embeddable
public class ObjectRetirementDO extends ValueObject {
...
    @Parent
    @Column(name = "objectid")
    public DomainObject getObject() {
        return _object;
    }
...
    @ManyToOne(targetEntity = com.hli.le.model.persistence.internal.domain.VersionDO.class)
    @JoinColumn(table = "ObjectRetirement", name = "versionid")
    public VersionDO getVersion() {
        return _version;
    }
...
    @ManyToOne(targetEntity = com.hli.le.model.persistence.internal.domain.ReasonDO.class)
    @JoinColumn(table = "ObjectRetirement", name = "reasonid")
    public ReasonDO getReason() {
        return _reason;
    }


Thanks!


Top
 Profile  
 
 Post subject: Re: Embedded object not loading when some fields are NULL
PostPosted: Wed Nov 03, 2010 7:14 pm 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
Is this a bug? I can't find enough documentation on @Embeddable objects to see if I'm misusing it.


Top
 Profile  
 
 Post subject: Re: Embedded object not loading when some fields are NULL
PostPosted: Thu Nov 04, 2010 11:36 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Just to understand this better; is the relationship from the embeddable class bi-directional? If so, could you please let me know more information about the other end of the relationship from the Embeddable object?


Top
 Profile  
 
 Post subject: Re: Embedded object not loading when some fields are NULL
PostPosted: Sat Nov 06, 2010 11:54 am 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
Yes, it is a bi-directional relationship. The ObjectRetirement table has an objectid field that is a FK to the Objects table's PK. Not sure what else you're asking for.


Top
 Profile  
 
 Post subject: Re: Embedded object not loading when some fields are NULL
PostPosted: Tue Nov 09, 2010 5:02 pm 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
I believe I understand the issue now. When implementing a class as an embeddable, Hibernate is creating a left outer join on that table selecting only the two nullable fields. Because those values are null, nothing is returned and the object is null (despite their being a record in the table).

I was able to work around this by mapping the foreign key field as a regular old @Column. Since that column is always populated, the query picks up the entire row and constructs the object properly.


Top
 Profile  
 
 Post subject: RFE: Option for setting @Embedded field null
PostPosted: Mon Sep 17, 2012 6:39 pm 
Newbie

Joined: Sun Aug 16, 2009 11:48 am
Posts: 4
RFE: Option for setting @Embedded field null when all columns are NULL
https://hibernate.onjira.com/browse/HHH-7610


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