-->
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.  [ 8 posts ] 
Author Message
 Post subject: Question about one-to-one mapping using foreign generator
PostPosted: Sat Apr 02, 2005 1:45 am 
Newbie

Joined: Sat Apr 02, 2005 1:30 am
Posts: 5
I have read through the manual and didn't find a solution for this. using Hibernate 3.0 and eclipse 3.1 M5 in my case.

I have two classes Parent and Child, using the same primary key for association. The Parent class primary key is "assigned" using sequence. The Child class should use the same key as primary key. The Child class contains no reference property to Parent class.

<class name="Parent" table="PTable">
<id column="pid">
<generator class="sequence">
<param name="sequence">parent_seq</param>
</generator>
</id>
<one-to-one name="child" class="Child"/>
<property name="other_prop" ....></property>
</class>

<class name="Child" table="CTable">
<id column="pid">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
<property name="child_other_prop" ... ></property>
</class>

My question is without Child class property reference to Parent, How could I still write a valid mapping file. Currently the mapping file for Child class will result in Exception "cannot resolve property [name=Parent] to corresponding index". I understand it is because the generator cannot find the Parent reference property in Child class. But how could I work around this? The class is pre generated in castor and table is defined as well to satisfy 3NF etc. Without freedom to change either class/table definition, could I still write a valid hbm.xml?

thanks in advance,

edmond


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 02, 2005 1:49 am 
Newbie

Joined: Sat Apr 02, 2005 1:30 am
Posts: 5
reformatted the xml mapping part here.
Code:
<class name="Parent" table="PTable">
    <id column="pid">
        <generator class="sequence">
            <param name="sequence">parent_seq</param>
         </generator>
     </id>
     <one-to-one name="child" class="Child"/>
     <property name="other_prop" ....></property>
</class>

<class name="Child" table="CTable">
    <id column="pid">
        <generator class="foreign">
            <param name="property">Parent</param>
        </generator>
    <property name="child_other_prop" ... ></property>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 02, 2005 3:34 am 
Newbie

Joined: Fri Apr 01, 2005 1:05 am
Posts: 10
Location: China
edmond_mm wrote:
reformatted the xml mapping part here.
Code:
<class name="Parent" table="PTable">
    <id column="pid">
        <generator class="sequence">
            <param name="sequence">parent_seq</param>
         </generator>
     </id>
     <one-to-one name="child" class="Child"/>
     <property name="other_prop" ....></property>
</class>

<class name="Child" table="CTable">
    <id column="pid">
        <generator class="foreign">
            <param name="property">Parent</param>
        </generator>
    <property name="child_other_prop" ... ></property>
</class>


I think parent.hbm.xml should like that:

<class name="Parent" table="PTable">
<id column="pid">
<generator class="sequence">
<param name="sequence">parent_seq</param>
</generator>
</id>
<one-to-one name="child" class="Child" constrained="true"/>
<property name="other_prop" ....></property>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 02, 2005 9:31 am 
Newbie

Joined: Sat Apr 02, 2005 1:30 am
Posts: 5
Yuan,

It is still the same error. I thought the constrained attr should be in Child class if the Parent to Child is 1:0/1 relationship. But anyway, the Exception is still property cannot be resolved in Child class id foreign generator


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 4:03 pm 
Newbie

Joined: Wed Jan 05, 2005 12:30 pm
Posts: 17
Location: Philadelphia, PA
As I understand it the "param" entry, it is not a property declaration, it is only a property reference. Therefore the child needs a property named "parent" for it to refer to.

Parent-mapping.xml:

Code:
<one-to-one name="child" class="ChildClass"/> 


Child-mapping.xml:

Code:
<one-to-one name="parent" class="ParentClass" constrained="true"/>


With both of these property declarations in place your param setting should be fine.

_evan


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 10:04 pm 
Newbie

Joined: Sat Apr 02, 2005 1:30 am
Posts: 5
Evan,

My situation is that the code was generated with castor source generator and it is not likely I can alter the Child class definition to add a property "parent". Can I still get a working hibernate mapping?

thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 11:17 pm 
Newbie

Joined: Wed Jan 05, 2005 12:30 pm
Posts: 17
Location: Philadelphia, PA
Quote:
Can I still get a working hibernate mapping?


Yes, but perhaps not with "foreign".

The definition of "foreign" in the reference docs is "uses the identifier of another associated object." There must be an association.

Can your generator make two passes? Can you live with the hassle of using the "assigned" generator on the child?

I am not sure I understand how, in a correct domain model, an object (child) can be dependent upon another object (parent) to establish uniqueness, yet cannot navigate to that object (child --> parent).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 12:11 am 
Newbie

Joined: Sat Apr 02, 2005 1:30 am
Posts: 5
Evan

Here I am using foreign generator, not foreign key. The association is through primary key. The Parent class primary key is via DB sequence generator, the Child class primary key is "foreign". Neither Child/Parent defined a property for such primary key. Ususally this can be done using composition and store both Child/Parent into the same table, but I cannot, since I need to store them into two tables (PTable/CTable).


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