I need to map two many-to-one Entities; one of this entities contains the other entity primary key as part of its own primary key.
We have three TABLEs "PERSON", "STATUS", and "SUB_STATUS" with the following structure:1. STATUS :Code:
COLUMN_NAME | IS_PRIMARY_KEY | IS_FORIGN_KEY | CAN_BE_NULL
------------------------------------------------------------------
STATUS | TRUE | FALSE | FALSE
------------------------------------------------------------------
NAME | FALSE | FALSE | FALSE
------------------------------------------------------------------
DESCRIPTION | FALSE | FALSE | TRUE
------------------------------------------------------------------
2. SUB_STATUS:Code:
COLUMN_NAME | IS_PRIMARY_KEY | IS_FORIGN_KEY | CAN_BE_NULL
------------------------------------------------------------------
STATUS | TRUE | TRUE| FALSE -----> references STATUS Table.
------------------------------------------------------------------
SUBSTATUS | TRUE | FALSE | FALSE
------------------------------------------------------------------
NAME | FALSE | FALSE | FALSE
------------------------------------------------------------------
DESCRIPTION | FALSE | FALSE | TRUE
------------------------------------------------------------------
3. PERSON:Code:
COLUMN_NAME | IS_PRIMARY_KEY | IS_FORIGN_KEY | CAN_BE_NULL
------------------------------------------------------------------
ID | TRUE | FALSE | FALSE
------------------------------------------------------------------
SUBSTATUS | FALSE | TRUE | TRUE -----> this column along with STATUS COLUMN references SUB_STATUS Table.
------------------------------------------------------------------
STATUS | FALSE | TRUE | FALSE -----> references STATUS Table.
------------------------------------------------------------------
NAME | FALSE | FALSE | FALSE
------------------------------------------------------------------
Now I need a proper mapping that allows me to load the person entity from database and access all of its values including the status and substatus;
my problem is n mapping the Person entity as illustrated above the SubStatus might be null so I need to keep both mapping on both entities Status and substatus.
the following are the mapping for my entities:Status Mapping:Code:
<hibernate-mapping default-cascade="none">
<class name="org.aliabdelaiz.business.entity.person.StatusImpl" table="STATUS" dynamic-insert="false" dynamic-update="false">
<id name="status" type="java.lang.String" unsaved-value="null">
<column name="STATUS" sql-type="VARCHAR2(255)"/>
<generator class="assigned">
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME " not-null="true" unique="false"/>
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="false" unique="false"/>
</property>
<set name="subStatuses" order-by="STATUS_FK" lazy="true" fetch="select" inverse="true">
<key foreign-key="SUBSTATUS_STATUS_FKC">
<column name="STATUS_FK"/>
</key>
<one-to-many class="org.aliabdelaiz.business.entity.person.SubStatusImpl" not-found="exception"/>
</set>
</class>
</hibernate-mapping>
SubStatus Mapping:Code:
<hibernate-mapping default-cascade="none">
<class name="org.aliabdelaiz.business.entity.person.SubStatusImpl" table="SUBSTATUS" dynamic-insert="false" dynamic-update="false">
<composite-id name="SubStatusPk" class="org.aliabdelaiz.business.entity.person.SubStatusPK">
<key-property name="subStatus" type="java.lang.String">
<column name="SUBSTATUS" sql-type="VARCHAR2(255)" not-null="true"/>
</key-property>
<key-many-to-one name="status" class="org.aliabdelaiz.business.entity.person.StatusImpl" >
<column name="STATUS"/>
</key-many-to-one>
</composite-id>
<property name="name" type="java.lang.String">
<column name="NAME " not-null="true" unique="false"/>
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="false" unique="false"/>
</property>
</class>
</hibernate-mapping>
the following is the mapping I need correction for it:Person Mapping:Code:
<hibernate-mapping default-cascade="none">
<class name="org.aliabdelaiz.business.entity.person.PersonImpl" table="PERSON" dynamic-insert="false" dynamic-update="false">
<id name="id" type="java.lang.String" unsaved-value="null">
<column name="ID" sql-type="VARCHAR2(255)"/>
<generator class="assigned">
</generator>
</id>
<many-to-one name="subStatus" class="org.aliabdelaiz.business.entity.person.SubStatusImpl" foreign-key="SUB_STATUS_FKC" not-null="false" lazy="proxy" fetch="select">
<column name="SUBSTATUS"/>
<column name="STATUS"/>
</many-to-one>
<many-to-one name="status" class="org.aliabdelaiz.business.entity.person.StatusImpl" foreign-key="STATUSC" not-null="true" lazy="proxy" fetch="select">
<column name="STATUS" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
this mapping guarantees that I can access the status even if the substaus is null.
but this mapping is in correct as it generates the following exception:
Code:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: org.aliabdelaiz.business.entity.person.PersonImpl column: STATUS (should be mapped with insert="false" update="false")