-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping table row in different ways based on discriminator
PostPosted: Tue May 28, 2013 2:55 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
I am looking for an elegant way to map the following scenario with Hibernate.

Table 'A' has a one-to-many relationship with B (many B rows per one A row). B rows come in two types - 'Black' and 'White'. This type is designated by an integer discriminator column in B.

There will always be exactly one Black B row, and many White B rows. I'd therefore like to map Black B as a separate property on A, and map the remaining White Bs as a list.

I did a very quick test where I tried applying a table-per-class-hierarchy mapping strategy, but I can't get the one-to-one part to work yet:

Code:
<class name="test.class.A" table="A_TABLE">
    <one-to-one name="blackItem" class="test.class.B" property-ref="parentA" />

    <set name="whiteItems" inverse="true">
        <key column="OID" />
        <one-to-many entity-name="WhiteB" />
    </set>
</class>


<class name="test.class.B" table="B_TABLE" discriminator-value="1">
    <id name="oid" type="long" column="OID">
        <generator class="sequence">bla</generator>
    </id>

    <discriminator column="B_TYPE_CODE" type="integer" />

    <many-to-one name="parentA" class="test.class.A" cascade="save-update">
        <column name="A_OID" not-null="true" />
    </many-to-one>

    <subclass name="test.class.B" discriminator-value="2" entity-name="WhiteB" />
</class>


When I try to persist this, Hibernate is trying to insert NULL into the A_OID column in Black B.

Am I on the right track, or is there a better way to do this? Any suggestions would be appreciated.


Top
 Profile  
 
 Post subject: Re: Mapping table row in different ways based on discriminator
PostPosted: Tue May 28, 2013 4:50 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
I got a bit further. Turns out that the problem with one-to-one was a fault in my test data. Now that I fixed that, I can successfully persist the objects.

The new problem is with finding A. When I retrieve A from the db, the A.whiteItems collection contains both the White and the Black B rows. The one-to-one correctly retrieves BlackB row, so BlackB ends up in both places.


Top
 Profile  
 
 Post subject: Re: Mapping table row in different ways based on discriminator
PostPosted: Tue May 28, 2013 6:01 pm 
Newbie

Joined: Wed Jun 22, 2005 3:58 am
Posts: 14
ok, really not sure why the descriminator is not being used when retrieving the rows, but a workaround is to use the 'where' attribute on the set:

Code:
<class name="test.class.A" table="A_TABLE">
    <one-to-one name="blackItem" class="test.class.B" property-ref="parentA" />

    <set name="whiteItems" inverse="true" where="B_TYPE_CODE=2">
        <key column="OID" />
        <one-to-many entity-name="WhiteB" />
    </set>
</class>


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