-->
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.  [ 4 posts ] 
Author Message
 Post subject: HELP NEEDED.dont know how to map if there is no primary key
PostPosted: Sun Mar 26, 2006 1:39 pm 
Newbie

Joined: Mon Feb 06, 2006 12:25 am
Posts: 17
Hi All,

I am trying to find a solution for couple of days.

mine is not a legacy database. i am using oracle.

i have a parent and child table . my child table does not have a primary key.

it has only foreign key. So which column you will specify in <id> tag of child hbm file.


i specified foriegn key in <id> tag of child hbm.

like:
<id name="foreignkey" column="FOREIGN_KEY">

and i also tried giving some other column which is in that child table.
<id name="age" column="AGE">
but the result i am getting is wrong.


for example:
i specified foreign key in <id> tag of child hbm. when i execute query for that id.

if there is two records for that id in child table.

when i display iam displaying two rows but the problem is first row printed twice. and the second original row is not showing. it means it overwrite the output with first record.


please if any body know this answer me.


thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 11:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want to map rows in the child table as entities then there must be a combination of columns that together make a unique row identifier. For example, the foreign key plus a sequence of some sort. The unique row identifier is what you set up as the primary key in the mapping... so long as every column in that combination is specified as being not-null.

If there is no such unique identifier (i.e. one parent can have two identical children.. you're mapping a bag) then you can't map the child as a stand alone entity. The best you can do is map it as a composite element: see the last example in section 6.2.4, "Collections of values and many-to-many associations", of the ref docs.

Given that this is not a legacy table, then I recommend that you read a few books, or at least cheat sheets, on database design. There is no good reason (and very few bad reasons) to create a table without a real primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 4:28 pm 
Newbie

Joined: Mon Feb 06, 2006 12:25 am
Posts: 17
then how will you define your child mapping document.

which column will you specify in <id> tag of child.hbm.xml??


since the child table dont have primary key, unique key.

Which column to map with <id> tag of child hbm.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 6:00 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
To quote the first line of my previous post:
tenwit wrote:
If you want to map rows in the child table as entities then there must be a combination of columns that together make a unique row identifier.

You have stated that there is no such combination of columns that uniquely defines a row in this table. Therefore those rows cannot be mapped as entities. You can map them as elements in a collection, or you can change your DB schema.

Rows must have an id at the database level if they are to have an id in their mapping: common sense. Otherwise, how would you look up one specific row? The thing is, you don't need to look up one specific row. You only need to look up all rows that belong to a given parent. So you don't need a child.hbm.xml at all. Search through the ref docs for "<element" and "<composite-element" for examples of how to map 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.  [ 4 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.