-->
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.  [ 14 posts ] 
Author Message
 Post subject: 2 fk's, one colum. How?
PostPosted: Fri Feb 25, 2005 10:45 am 
Newbie

Joined: Fri Jan 09, 2004 5:41 pm
Posts: 14
Hi,
I'm having trouble with mapping a relationship. I have a table with 3 fk's.
2 of share the same column. The way I have this mapped is not working.

The mapping:

<many-to-one
class="Patient"
name="Patno"
not-null="true"
>
<column name="patno" />
<column name="internal_protocol_id" />
</many-to-one>
<many-to-one
class="Protocol"
name="InternalProtocol"
not-null="true"
>
<column name="internal_protocol_id" />
</many-to-one>

The error:
ERROR: Repeated column in mapping for class com.fujisawa.webforms.app.Form
should be mapped with insert="false" update="false": internal_protocol_id


Thanks,
-Scott


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 6:46 pm 
Beginner
Beginner

Joined: Fri May 21, 2004 12:21 pm
Posts: 22
Location: Sacramento CA, USA
This error means you mapped this field "internal_protocol_id" 2 times in your mapping file.
You can't map it two times, so in one of them you have to specify insert and update as false.
Or if you mapped it wrong take it out.

Hope this works


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 26, 2005 1:44 am 
Newbie

Joined: Fri Jan 09, 2004 5:41 pm
Posts: 14
If do specify it as update false for one of the mappings, will I not take the risk of violating a fk constraint for that mapping? I would think it would be common to have a colum in one table be a foreign key to more than one table. You have to map it once for each relationship, correct? I didn't find any examples of this in the users guide.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 26, 2005 11:56 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
sstaley wrote:
I would think it would be common to have a colum in one table be a foreign key to more than one table.

I believe foreign keys should each refer only to one table. That's the purpose of them: the key refers to a unique row, which of course cannot exist in two tables at once.
Why not add another column for the other relation?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 26, 2005 2:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It is very seldom (read broken model) to have one fk point to two different entities. What decides which entity is the right one ? Or are they both valid at the same time ? ...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 12:16 pm 
Newbie

Joined: Fri Jan 09, 2004 5:41 pm
Posts: 14
I'll try to explain more clearly. The table has 2 foreign key constraints; each to different tables. Once of them is composed of column A and B and the other is only column A.

Is the model broken to conceptually want to only allow rows in a table if there are existing rows in 2 other tables?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 12:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It *is* a broken data model I think. Properly normalized models don't have this, I'm pretty certain.

But, its easy to handle, just follow the instruction in the exception message.

Quote:

"Repeated column ... should be mapped with insert="false" update="false": internal_protocol_id"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 12:31 pm 
Newbie

Joined: Fri Jan 09, 2004 5:41 pm
Posts: 14
Ok, makes sense. I'm working with an old sybase database and it's not normalized at all. THanks for all the help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 6:22 pm 
Newbie

Joined: Mon Feb 28, 2005 5:46 pm
Posts: 7
I don't completely agree with all the above as I am having the exact problem as sstaley and feel the db design represents the data well.

I have a timesheet apllication with applicable table structures as follows:
TIMESHEET (
timesheet_id [pk],
employee_id [fk],
end_date,
submitted,
approved
)

TIMESHEET_TASK(
timesheet_task_id [pk],
timesheet_id [fk],
subtask_id [fk],
task_id [fk],
...
)

TASK(
task_id [pk],
task_name
)

SUBTASK(
subtask_id [pk],
task_id [pk],
subtask_name,
...
)

and the corresponding mapping segments are as follows:

TimesheetTask.hbm.xml
Code:
<many-to-one name="subtask" foreign-key="FK_TIMESHEET_TASK_SUBTASK" class="QT.QTSubtask">
                    <column name="SUBTASK_ID"/>
                    <column name="TASK_ID"/>
                </many-to-one>
      <many-to-one name="task" column="TASK_ID" foreign-key="TASK_ID_FK" class="QT.QTTask"/>


Subtask.hbm.xml

Code:
<composite-id>
                    <key-property name="subtaskId" column="SUBTASK_ID" type="java.lang.String"/>
                    <key-property name="taskId" column="TASK_ID" type="java.lang.String"/>
                </composite-id>


There are some important points to note regarding the data. A subtask cannot logically exists without a corresponding task -> task-to-subtask is a one-to-many relationship. TIMESHEET_TASK must use TASK_ID 2x as a foreign key (this is essentially what I'm asking for help on) since I must be able to add both a subtask and a task upon entering a timesheet_task. Note: tasks are required for adding a timesheet_task while subtasks are not.

I thought about appending a system generated id to the SUBTASK table that would eliminate the problem altogether, however something doesn't sit right with me in creating an id field with no apparent meaningfulness with the data. Any and all thoughts are welcome and appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 7:02 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
I guess I would recommend doing things a simply as possible. This kind of design seems needlessly complex, as there are straightforward ways of modeling such relationships.

My questions about your design are:

1) Are task and subtask in the same table? If so, why, if they are different entities storing differing information? If not, why can't subtask use its subtaskId field as an id instead of the composite-id?

2) If subtasks are children of tasks, why does the TimetableTask have a collection of them? Shouldn't they be accessed through the Task?

Perhaps I am misunderstanding. What do your tables look like?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:28 am 
Newbie

Joined: Mon Feb 28, 2005 5:46 pm
Posts: 7
The table structures are listed in my first post above the hibernate mappings. I've been staring at this for a couple days now so my fresh perspective has somewhat dissolved.
1) The answer is yes and no, as you'll see from the table structures. Task's CAN exist without subtasks, but not vice-versa. Similarly, you can attach a task to a timesheet_task (a timesheet_task represents a task that has been attached to a timesheet with additional information regarding the date and billlable options and furthermore references a table containing the # of hours worked on that task item for a given day of the week), but you cannot attach a subtask without a task. Here is some example data that will perhaps make this a bit easier to visualize.

Administration Tasks Screen:
DEV - Development
MKT - Marketing
ADMIN - Administration

Administration Subtasks Screen:
(if you select DEV - Development as the parent task from a drop-down menu, you'd have a listing of the corresponding subtask options)
BUG - Bug Fix
CLER - Clerical
CODE - Code
DB - Database

(or if you select ADMIN, you might only have these options as subtasks)
ID - Name
ACCT - Accounting
CLER - Clerical
TRATIM - Travel Time

Remember, the above are the admin screens where the associations would be set up. Next is the actual user interface on the timesheet screen.

Timesheet Display:
TimesheetId = 54, ending date = 03/05/05

(first task)
TimesheetTaskId = 67 (hidden field)
MyCompany, Inc. - Development - Code [billable checkbox] [hours sunday] [hours monday]... [hours saturday]

(second task)
TimesheetTaskId = 68 (hidden field)
MyCompany, Inc. - Administration [billable checkbox] [hours sunday] [hours monday]... [hours saturday]

Notice for the first task I list the customer name, task name, and subtask name but for the second task there is a customer and task name but no subtask name. You can add a timesheet task without a subtask as well and since a subtask can't exist from an administrative (see above) standpoint without a corresponding task, that's where things get hairy.

I think what I will do, considering I'm coming into this project and making modifications to an existing system, is append a system generated id field to the subtask table. That way I can refer to both Tasks and Subtasks independently in the timesheet_task table so as to avoid the hibernate mapping issue. If any of this makes sense, what are your thoughts on this revision? If this still doesn't jive I can always try to take screen shots and post them to make what I'm trying to say even more lucid.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:37 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
I guess I would only make it a tad simpler. Change:

SUBTASK(
subtask_id [pk],
task_id [pk],
subtask_name,
...
)

to

SUBTASK(
subtask_id [pk],
task_id [fk],
subtask_name,
...
)

since in fact the task_id is in a different table. No need for an extra column if you have subtask_id as the sole id.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 11:07 am 
Newbie

Joined: Mon Feb 28, 2005 5:46 pm
Posts: 7
I discovered an issue between the design and requirements. The original spec was calling for a one-to-many relationship between task and subtask when in reality (and i was right on this) it was a many-to-many. I'm removing task_id from the subtask table and creating a join table between task and subtask for many-to-many relationship. Thank you very much for your time, mgreer. Without describing this all to a third party who knows how long it would have been before that disparity was discovered.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 11:13 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
No trouble. Good luck!


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