-->
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.  [ 10 posts ] 
Author Message
 Post subject: Mapping Question (Composite Keys)
PostPosted: Wed Mar 29, 2006 4:34 pm 
Newbie

Joined: Fri Nov 05, 2004 1:06 pm
Posts: 9
Could someone please confirm whether this is possible or not (after spending quite a bit of time looking at the documentation I don't think it is).

Table Task has a composite key [processName,State]
Table Entity has a surrogate primary key.
Table Entity also has two columns[scenarioName,State] (which do not constitute a key) that match the composite key in Task.

Can I create a one-to-many relationship from Entity to Task?

The relationship is in fact a many to many relationship but I am only interested in the one-to-many part (from Entity to Task), the reason is this, if I make it many to many then I need to manage the relationship (via the link table), if however it is a one to many relationship then if the attributes that make up the key in Entity change I will automatically get a new set of tasks. If the relationship is many to many, I will need to manually update the tasks each time the state changes.

I tried the following, to no avail:

<class name="Entity" table="WORK_ENTITY">

<id name="id" column="ID">
<generator class="native"/>
</id>

<properties name="key">
<property name="scenarioName" type="java.lang.String"/>
<property name="state" type="java.lang.String"/>
</properties>

<set name="nonWfTasks" lazy="false">
<key property-ref="key"/>
<one-to-many class="WorkEntityNonWFTasks"/>
</set>
</class>

<class name="WorkEntityNonWFTasks" table="NONWF_TASKS">

<composite-id>
<key-property name="processDefName" type="java.lang.String" column="PROCESSDEFNAME"/>
<key-property name="entityState" type="java.lang.String" column="ENTITYSTATE" />
</composite-id>

</class>

Exception:
collection foreign key mapping has wrong number of columns: com.gs.fw.cmpl.em.gwec.persistence.workentity.model.WorkEntity.nonWfTasks type: component[scenarioName,state]

The thing is that I attempt to refefine my key using the composite properties object for the FK mapping. I would very much appreciate it if someone could let me know if this is even possible or am I wasting my time.

Thanks
David


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 8:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Just map the columns as a sub-tag of key;

Code:
<class name="Entity" table="WORK_ENTITY">

<id name="id" column="ID">
<generator class="native"/>
</id>

<property name="scenarioName" type="java.lang.String" update="false" insert="false"/>
<property name="state" type="java.lang.String" update="false" insert="false"/>

<set name="nonWfTasks" lazy="false">
    <key>
      <column name="ScenarioName"/>
      <column name="State"/>
    </key>
    <one-to-many class="WorkEntityNonWFTasks"/>
</set>
</class>


Use this as a starting point then change approrpiately.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It certainly is possible. All you've done wrong is specify the set's key incorrectly. Try this:
Code:
<set name="nonWfTasks" lazy="false">
<key>
  <column name="scenarioName"/>
  <column name="state"/>
</key>
<one-to-many class="WorkEntityNonWFTasks"/>
</set>
You don't need to specify property-ref, becasue you're joining on WorkEntityNonWFTasks' primary key. property-ref always refers to columns in the other table, and you'd use it if the columns in WorkEntityNonWFTasks that Entity uses to join to aren't its primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 9:20 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Dang, that'll teach me to have lunch between starting and ending writing a message :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 8:05 am 
Newbie

Joined: Fri Nov 05, 2004 1:06 pm
Posts: 9
Guys,
Thanks so much for the responses, however I am a little confused:

<Class name="WorkEntity" ...

<set name="nonWfTasks" lazy="false">
<key>
<column name="scenarioName"/>
<column name="state"/>
</key>
<one-to-many class="WorkEntityNonWFTasks"/>
</set>
</Class>

In this relationship, does the key (child of set) not refer to the FK in the WorkEntityNonWFTasks? and is this key not then mapped to the primary key of the WorkEntity table? Is this not how the 1 to many relationship works in hibernate?

Also, apologies but I made a mistake in my original post; the key of the WorkEntityNonWFTasks is in fact:

ScenarioName
State
TaskName

So the relationship (as I see it) is:

FK ........... Key (not PK).
Task.ScenarioName Entity.ScenarioName
Task.State Entity.State

Once again, thanks for the response, any and all help is greatly appreciated.

David


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 5:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The <key> element refers to the columns in WorkEntity's table that relate to columns, probably the primary key, in nonWfTasks. For one-to-many, the columns in WorkEntityNonWFTasks are never specified: hibernate uses either the primary key, or the columns specified in WorkEntityNonWFTasks's many-to-one back to WorkEntity (if that's specified).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 8:24 pm 
Newbie

Joined: Fri Nov 05, 2004 1:06 pm
Posts: 9
If you have a one to many relationship (in this case WorkEntity has a one to many relationship to Tasks) how can it work if the one side does not specify which key(s) act as the foreign key in the associated table (there is no many to one relationship on the other side)

If the one side of the relationship specifies the primary key, what columns define the foreign key, am I missing something fundamental?

You say that the <key> element maps to the primary key of WorkEntity, but surely this must match the defined foreign keys in the many relationship, but where are these specified?

Cheers,
David


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 8:47 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There must have been a terminology mishap somewhere. Rereading my post confuses me :) Let's try this with examples.

Parent table: Columns ParentId, ParentName
Child table: Colulmnd ChildId, ParentIdInChild, ChildName.

Mapping:
Code:
<class name="Parent" table="Parent">
  <id name="ParentId" ...>
  <property name="Name" column="ParentName"/>
  <set name="Children" inverse="true">
    <key column="ParentIdInChild"/>
    <one-to-many class="Child"/>
  </set>
</class>
<class name="Child" table="Child">
  <id name="ChildId" ...>
  <property name="Name" column="ChildName"/>
  <!-- Can drop this many-to-one safely -->
  <many-to-one name="Parent" class="Parent" column="ParentIdInChild"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 8:57 pm 
Newbie

Joined: Fri Nov 05, 2004 1:06 pm
Posts: 9
Thanks for persisting:

If the ParentIdInChild column(s) refers to actual columns in the child table that are acting as a foreign key to the primary key in the parent thable then I am clear, this makes sense to me. Is this what you are saying?

Thanks again :)
David


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 9:07 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yep. Which is why in your original question, <key property-ref="key"/> was wrong: the "key" property referred to columns in the parent table, not in the child table.


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