-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: My understanding of composite key is wrong?
PostPosted: Thu Jan 20, 2005 2:59 am 
Newbie

Joined: Mon Jan 17, 2005 9:05 am
Posts: 17
I am reading hibernate documents now. I read something about the composite id "You may use a component as an identifier of an entity class. Your component class must satisfy certain requirements:

It must implement java.io.Serializable.

It must re-implement equals() and hashCode(), consistently with the database's notion of composite key equality.

You can't use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.

"

That means the following scenario could not be fulfilled by hibernate?

A (

ID NUMBER(19),
Name varchar2(255) ,
primary key (ID)
)

B (
ID NUMBER(19),
VERSION NUMBER(10),
primary key (ID, VERSION)
)

A's ID is the same with B's ID. If reference from the above docs, that means my composite key in table B could not reference A's ID? right?

Am I wrong? I think it is a very common scenario in database structure. So how to solve this problem using hibernate?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 4:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Code:
<composite-id name="id" class="BId">
    <key-many-to-one name="A"/>
    <key-property name="verision"/>
</composite-key>
[/code]

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 6:24 am 
Beginner
Beginner

Joined: Wed Dec 08, 2004 9:17 am
Posts: 25
Max,

we have almost the same problem in our project. We use the same type of composite primary keys in our db tables. Because we have a java domain model that we are not free to change (at the moment JDO is used and works well at least with the domain model), we are not able to introduce extra classes for the composite ids, i.e. the composite ids have to reside as properties within the domain classes.

How can one solve the problem in this case?

A typical mapping looks like this:

Code:
<hibernate-mapping schema="dbo" package="de.vodafone.rnp.core.business.persistence.hibernate">
    <class name="CellGroupImpl" table="funzgr" proxy="CellGroupImpl" lazy="true">
        <composite-id>
            <key-property name="workspaceId"/>
            <key-property name="versionId"/>
            <key-property name="name"/>
        </composite-id>
        <property name="workspaceId" type="integer" column="arb_lfdnr" insert="false" update="false"/>
        <property name="versionId" type="integer" column="ver_nr" insert="false" update="false"/>
        <property name="name" type="string" column="zgr_id"/>
       
        <set name="cells" table="vsto_funzgm" lazy="true" inverse="true">
            <key>
                <column name="arb_lfdnr_1"/>
                <column name="ver_nr_1"/>
                <column name="zgr_id"/>
            </key>
            <many-to-many class="CellImpl">
                <column name="arb_lfdnr_2"/>
                <column name="ver_nr_2"/>
                <column name="nls_id"/>
                <column name="sta_id"/>
                <column name="sta_loc"/>
                <column name="alt_nr"/>
                <column name="fan_lfdnr"/>
                <column name="zel_id"/>
            </many-to-many>
        </set>
        <many-to-one name="version" class="VersionImpl">
            <column name="arb_lfdnr"/>
            <column name="ver_nr"/>
        </many-to-one>
    </class>
</hibernate-mapping>


workspaceId and versionId are "inherited" ids from the class "one step up in the hierarchy" , i.e. VersionImpl.
A colleague of yours told me to remove the property elements for workspaceId, versionId and name and put the type and column information into the key-property element.
If I do that, Hibernate will abort processing the mapping telling me that I have to set insert= false and update= false for the repeated columns arb_lfdnr and ver_nr.
But this is not possible because these attributes are not allowed for the elements "column" or "key-property". It is only possible for the "property" element which I was told to remove.

Is the composite id scenario that the initial poster described only manageable for Hibernate if I introduce extra classes for the composite ids?

Any help would be greatly appreciated 'cause I have posted quite some questions regarding this topic in the last few days and still haven't received any answer/solution to this problem.

Thanks a lot!

Cheers,

Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 6:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
<key-property> and <key-many-to-one> allows you to have nested columns on which you can specify all that stuff.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 6:38 am 
Newbie

Joined: Mon Jan 17, 2005 9:05 am
Posts: 17
max,

I tried to use the 2 tag you mentioned on my test, however, I did not get any success. I search a few days on this forum, a lot of people encountered the same question as me. But I did not see any solution out there.

My mapping file is ok, but when I try to run and save the parent , it throw out exception.

Another problem is before saving parent and child, their id key is null, then what should I put in the primary key class? should I initialize the key class?

I wonder if hibernate team could give an example code like the post i did above. So that whoever got this problem could find the answer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 6:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
try go looking for the compositeid test in Hibernate3 (it works very similar to H2)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 6:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sorry - i didn't catch that you wanted update/insert = false on the key properties. Why would you ever want that ?

Why do you duplicate the exact same properties in both the composite-id and in property tags ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 6:54 am 
Beginner
Beginner

Joined: Wed Dec 08, 2004 9:17 am
Posts: 25
Max,

the element "column" you are talking about does NOT have attributes "insert" and "update" which I can set to "false", to make things work.
So, HOW is it possible "to do all that stuff" as you wrote?

I, too, have searched the forum for days regarding how to handle composite ids/keys with Hibernate. I have found a lot of questions but hardly any post that answered these questions, not to speak about any kind of example. I think it would be great, if you guys could publish some example on the Hibernate web site "How to deal with composite keys/ids". You have great examples dealing with how to do things in a "typical, straight forward Hibernate scenario". So, how about an example about how to do things in a real world scenario with composite ids/keys that does not seem too uncommon?

Thanks in advance and best regards,

Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 7:00 am 
Beginner
Beginner

Joined: Wed Dec 08, 2004 9:17 am
Posts: 25
ooops, you where faster, max. ;-)

the problem is, that if I do not have dublicate elements "key-property" and "property" for the composite id properties, I am not able to specifiy "insert=false" and "update=false" attributes for the repeated columns (arb_lfdnr and ver_nr), which is what Hibernate tells me to do.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 8:46 am 
Beginner
Beginner

Joined: Wed Dec 08, 2004 9:17 am
Posts: 25
Ok, found the place...
Have to provide the attributes in the "many-to-one" element.

So, what is still missing now, is to find out how to do queries for objects with composite ids. Following the Hibernate doc I alwas get a
Code:
net.sf.hibernate.QueryException: path expression ends in a composite value: versionimp0_.id [from de.vodafone.rnp.core.business.persistence.jdo.VersionImpl as v where v.id=? AND v.workspaceId=?]

which doesn't tell me too much about what I am doing wrong. ;-)

Any ideas about this one?

Cheers,

Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 9:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
just for fun i googled on your error message, and look and behold (this is from the hibernate ref manual=:

Quote:
Never try to use a path-expression that ends in a property of component type (as opposed to a property of a component). For example, if store.owner is an entity with a component address

store.owner.address.city // okay
store.owner.address // error!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 9:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
btw. how did you mapping end up ? as i saw it you had the same column in multiple properties making it hard for hibernate to know which property is "judge".

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 9:49 am 
Beginner
Beginner

Joined: Wed Dec 08, 2004 9:17 am
Posts: 25
Hi Max,

please take a look at http://forum.hibernate.org/viewtopic.php?p=2228155#2228155.
It also describes more isolated the problem that I am having regarding the "path expression ends in a composite value" topic.
It would be great if you could tell me how the example you posted relates to my problem. I guess I am too blind to see the connection.

Thanks a lot!

Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 9:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
your composite id is named "id", but that does not identify which part of the id you want to compare/join on. you need to refer to each property in the composite id (id.yourkeyproperty)

p.s. have you considered commercial support ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 20, 2005 10:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
PS: Reposting the same stuff in multiple threads does not make it easier for us to answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.