• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dont know how to map if there is no primary key???

 
gopal kishan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[ March 28, 2006: Message edited by: Mark Spritzler ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

my child table does not have a primary key

A record without a primary key is not valid relational data, hence your problem. Give the child table a surrogate key, map this accordingly and you problem will go.
 
gopal kishan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to definen a surrogate key?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a field to your table which can act as a primary key. For example a pseudo-DDL for a table with a surrogate key might look like:

Only name and sex are attributes of person, but neither are good candidates for a primary key since they are either not unique or can change. So the id is the surrogate key; it is an attribute of person that exists only to identify a person. Make sense?
 
Mahesh Gurav
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

override equals method in child bo as shown below,two different rows will be there when u print.
public boolean equals(Object obj) {
Child batch = (Child ) obj;
return this.batchID.equals(batch.getBatchID()) ? true : false;
}
where batchID is PK of child table.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. What you are suggesting there is kind of how Hibernate works already (though I can't help but think that changing the identifying property away from the expected id property is liable to make Hibernate behave unreliably). The problem still exists that this child table has no primary key and a primary key is a fundamental, unavoidable requirement for relational data. No primary key and the data is not relational, so Hibernate cannot be expected to work.
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your original question it appears you already have a surrogate key, namely the column you call FOREIGN_KEY. Just put a primary key constraint on that column and voila, you have a primary key (it's quite alright for a primary key to also be a foreign key).
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...of course the consequence of that is you now have a one-to-one relationship between parent and child.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic