-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with composite-id and foreign key(parent/child rel.)
PostPosted: Thu Dec 29, 2005 6:22 pm 
Newbie

Joined: Thu Dec 29, 2005 5:57 pm
Posts: 5
Hi all,

I'm newbie to hibernate and couldnot find a solution for this problem:

I have a table fld_field to realize a parent/child(composite) relationship.
The tables primary key consists of 2 columns fld_oid_high and fld_oid_low(composite-id) and
the parent-relationship is realized through the the foreign key(s) fld_parent_high/ fld_parent_low.

That's the table:
CREATE TABLE fld_field(fld_oid_high BIGINT NOT NULL,fld_oid_low BIGINT NOT NULL,fld_parent_high BIGINT, fld_parent_low BIGINT,fld_name varchar(255) default '',fld_deleted INT default 0 NOT NULL,fld_ts timestamp DEFAULT now,PRIMARY KEY (fld_oid_high, fld_oid_low),
FOREIGN KEY (fld_parent_high, fld_parent_low) REFERENCES fld_field(fld_oid_high,fld_oid_low) )


The Hibernate Tools created the mapping below. My problem is that I need in the persistent class/POJO not the property 'parent' delivering the parent as class Field, but a property(fkOid) simply delivering the foreign key as composite class 'myorg.as.jlibs.oid.Oid'(columns fld_parent_high and fld_parent_low) - without fetching the columns in a extra sql!

I hope there is a possibilty in hibernate and thanks for your answers.

<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="Field" table="fld_field">
<composite-id name="oid" class="myorg.as.jlibs.oid.Oid">
<key-property name="high" type="long" column="fld_oid_high" />
<key-property name="low" type="long" column="fld_oid_low" />
</composite-id>
<many-to-one name="parent" class="Field" fetch="select">
<column name="fld_parent_high" />
<column name="fld_parent_low" />
</many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 6:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I had a little dificulty parsing that. Do you want Field.getParent() to return only the OID of the parent, and not the parent object? If you do, then eliminiate the many-to-one mapping and use a component. If you want to be able to access the parent's OID directly from the current Field object, then you could add getParentOID to the Field class (but not to the mapping); that would return getParent().getOid(). And if you change your parent fetch strategy to "join" (which incurs basically no overhead on classes this small), then the parent's OID will always be available without having to issue another select statement.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 7:20 pm 
Newbie

Joined: Thu Dec 29, 2005 5:57 pm
Posts: 5
tenwit wrote:
I had a little dificulty parsing that. Do you want Field.getParent() to return only the OID of the parent, and not the parent object? If you do, then eliminiate the many-to-one mapping and use a component. If you want to be able to access the parent's OID directly from the current Field object, then you could add getParentOID to the Field class (but not to the mapping); that would return getParent().getOid(). And if you change your parent fetch strategy to "join" (which incurs basically no overhead on classes this small), then the parent's OID will always be available without having to issue another select statement.


Well/yes, I simply want the current Field object return the OID of the parent: Field.getParentOid should return the class myorg.as.jlibs.oid.Oid with data/columns fld_parent_high+fld_parent_low.

I think it should be possible to declare such a property in the Hibernate mapping :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 7:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It is possible. You don't want a many-to-one, that's a reference. You want a value mapping: a component tag with property tags inside it.

Hibernate doesn't allow you to map columns twice. So if you want to be able to use both a many-to-one and a component mapping for the same columns (which is a fairly rare thing: most of the time, the many-to-one is preferable, then just get the OID from the contained object), then map the many-to-one in the normal way, then set up the component but use property's formula attribute instead of column attribute. This will allow hibernate to maintain the relationship for you, and still allow you to get the parent OID without loading the parent object.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 1:07 pm 
Newbie

Joined: Thu Dec 29, 2005 5:57 pm
Posts: 5
Thanks!

The problem is saved.

simply adding

<component name="parentOid" class="myorg.as.jlibs.oid.Oid">
<property name="high" column="fld_parent_high" insert="false" update="false" />
<property name="low" column="fld_parent_low" insert="false" update="false" />
</component>



to <class> element


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 4:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Glad it helped. You forgot to rate me though!


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