-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping two many-to-one Entities; one contains the other.
PostPosted: Mon May 30, 2011 10:51 am 
Newbie

Joined: Sat Jun 17, 2006 5:28 am
Posts: 8
Location: Egypt
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")

_________________
Ali Abdel-Aziz
http://www.aliabdelaziz.org/


Last edited by Ali Abdel-Aziz on Thu Jun 02, 2011 2:35 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Mapping two many-to-one Entities; one contains the other.
PostPosted: Mon May 30, 2011 12:47 pm 
Newbie

Joined: Sat Jun 17, 2006 5:28 am
Posts: 8
Location: Egypt
actually adjusting the person.status mapping to be as follows fixes the problem:
Code:
<many-to-one name="status" class="org.aliabdelaiz.business.entity.person.StatusImpl" foreign-key="STATUSC" not-null="true" lazy="proxy" fetch="select" insert="false" update="false">
                    <column name="STATUS" not-null="true"/>
</many-to-one>


but is there a way to mark the status column mapping as insert="false" update="false" inside the substatus mapping instead of the status mapping.

_________________
Ali Abdel-Aziz
http://www.aliabdelaziz.org/


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