-->
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: 1-to-1 mappings and subclassing
PostPosted: Mon Jul 19, 2004 4:23 pm 
Newbie

Joined: Mon Jul 19, 2004 3:34 pm
Posts: 4
I would appreciate help on a few related items - I've read the reference and many posts prior to writing this, but am unclear on a few things.

Here's my scenario: I have a House class mapped to table HOUSE, and 2 properties: CoOwner and PrimaryOwner. PrimaryOwner extends Owner (the base class, also used as type for the co-owner), and I'd like to use the joined subclass pattern (preferrable table structure). The PrimaryOwner is required; co-owner is not.

Here's my initial mapping:

Code:
<hibernate-mapping>
    <class name="com.mycompany.House" table="HOUSE">
        <id name="houseId" type="string" column="HOUSE_ID" length="7"/>
            <generator class="assigned"/>
        </id>
        <one-to-one name="primaryOwner" class="com.mycompany.PrimaryOwner"/>
        <one-to-one name="coOwner" class="com.mycompany.Person"/>

        <property name="houseValue" column="houseAmount" type="string"/>
        ...
    </class>

    <class name="com.mycompany.Person" table="OWNER">
        <id name="id" column="OWNER_ID" type="integer">
            <generator class="sequence">
                <param name="sequence">OWNER_ID_SEQ</param>
            </generator>
        </id>
        <many-to-one name="house" column="HOUSE_ID" class="com.mycompany.CLTProductForm"/>
        <property name="lastName" column="LAST_NAME" type="string" length="30"/>
        ...
    </class>

    <joined-subclass name="com.mycompany.PrimaryOwner"
                     table="PRIMARY_OWNER"
                     extends="com.mycompany.Person">
        <key column="PERSON_ID"/>
        <property name="idType" column="ID_TYPE" type="string" length="1"/>
        <property name="idValue" column="ID_VALUE" type="string" length="30"/>
    </joined-subclass>
</hibernate-mapping>


Now the questions:

1. I'd like to map both owners to the OWNER table (and the primary would also have a record in PRIMARY_OWNER), but can't use the HOUSE_ID as the primary key on OWNER because there are 2. So I'd prefer composite key (HOUSE_ID + OWNER_ROLE), where OWNER_ROLE is "P" for primary, "C" for co-owner, etc. (typical DB design). Each of those roles could also be in a reference table OWNER_ROLE. Any ideas on how such a "type" constant can be used to identify instances of a particular 1-to-1 class in a separate table? The HOUSE_ID would be part of the primary key, and also a foreign key reference to HOUSE. If I can't do that, I'll just have to keep the generated key as I have it (don't need one, except for Hibernate mapping).

2. If I use the mapping above, do I need to have an id property in the Person object? I don't need it; it would be for the primary key in the database, and I will be manipulating Person through House (at least for now).

3. In the above, I do get a HOUSE_ID column in the OWNER table, which is what I want; but if I make it a <one-to-one> mapping from Owner to House, I don't get that at all (maybe because I don't have a House attribute in Person). It should be one-to-one, though; part of my confusion over this is how I can keep my Person class pure, with no reference to House (since it could be used in other contexts).

Should I subclass Person into Owner, and give owner a type/role column, thus keeping Person "pure"? Has anyone done that type of thing?

3. I'd like to have CREATED_DATE and UPDATED_DATE columns in the database, but they also don't need to be in the object (as far as I'm concerned). Can I use <generator> types of constructs anywhere but in the <id>? If not, that would be useful for timestamping, update counts, etc.


Thanks for any information or suggestions you can give me... parital answers welcomed. sorry for the length of the post. I did try to edit it down.

- Eric


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 19, 2004 5:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I believe that this mapping is possible in current Hibernate3, and it is probably a trivial task to make it possible in Hibernate 2.1.x CVS.

However, I don't think its advisable. The trouble with the mapping is that you can't define proper foreign key constraints.

Here is how it would work:

Code:
   <class name="Person" table="OWNER" discriminator-value="c">
        <composite-id name="id">
            <key-property name="ownerId" column="OWNER_ID"/>
            <key-property name="ownerType" column="OWNER_TYPE"/>
        </composite-id>
        <discriminator formula="OWNER_TYPE" type="character"/>
        <property name="lastName" column="LAST_NAME" type="string" length="30"/>
        ...
        <subclass name="PrimaryOwner" discriminator-value="p">
              <property name="idType" column="ID_TYPE" type="string" length="1"/>
              <property name="idValue" column="ID_VALUE" type="string" length="30"/>
        </subclass>
   <class>

    <class name="House" table="HOUSE">
        <id name="houseId" type="string" column="HOUSE_ID" length="7"/>
            <generator class="assigned"/>
        </id>
        <many-to-one name="primaryOwner" insert="false" update="false" class="PrimaryOwner">
            <column name="HOUSE_ID"/>
            <column formula="'p'"/>
        </many-to-one>
        <many-to-one name="coOwner" insert="false" update="false" class="Person">
            <column name="HOUSE_ID"/>
            <column formula="'c'"/>
        </many-to-one>

        <property name="houseValue" column="houseAmount" type="string"/>
        ...
    </class>


But if I were you, I would consider a different data model; one which has true referential integrity.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:52 am 
Newbie

Joined: Mon Jul 19, 2004 3:34 pm
Posts: 4
Thanks very much - I agree with you, and this object design isn't my choice. I have limited refactoring capability (backward compatibility issues), so I'm trying to get a reasonable data model for now with limited design changes. Not the situation I prefer, but there you have it.


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.