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