-->
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.  [ 6 posts ] 
Author Message
 Post subject: Same primary key between parent and child table
PostPosted: Thu Dec 15, 2005 2:51 pm 
Newbie

Joined: Thu Dec 15, 2005 1:17 pm
Posts: 5
Hibernate version:3.0

Hi,
I have 2 tables,
Table - Person
int Person_Id (PK);

Table - Credential
int Person_ID (PK)(FK from Person Table);
varchar username;
varchar password

The relationship between Person(Parent) and Credential(Child) is a RDBMS identifying relationship with zero or one map. This means the PK in Credential is the same as the PK in Person table.

My Objects are
Object - Person
int personId; (PK)
Credential credential;

Object - Credential
int personId; (PK but same as PK from Person)
String username;
String password;

My config file is
person.hbm.xml

<class name="Person" table="PERSON">
<id name="personId" type="int" column="Person_Id" unsaved-value="0">
<generator class="increment"/>
</id>

<one-to-one name="credential" class="Credential" cascade="all"></one-to-one>
</class>
credential.hbm.xml
<hibernate-mapping package="com.vo">
<class name="Credential" table="CREDENTIAL">
<id name="personId" column="Person_Id"></id>
<property name="username" column="SSO_Username"/>
<property name="password" column="SSO_Password"/>
</class>
</hibernate-mapping>

When I try to retrieve the data everything works fine. When I try to insert a record I get the following error
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK__CREDENTIA__Perso__5006DFF2'. The conflict occurred in database 'TEST', table 'PERSON', column 'Person_Id'.

I request somebody to help me to point out what I am doing wrong. Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 3:17 pm 
Newbie

Joined: Sun Aug 07, 2005 3:55 am
Posts: 11
Location: slovakia
yeah ... think of it what happens: you save a Person it assings an identifier to it but the identifies is not assigned to the Credential entity an it has to be as You are cascading and the FK tham simply fails as there is no such credential.

You might try to do the following: it seems you are using method accessors so put the following code to Your Person class:

Code:

Person {

   private void setId(int id) {
      this.id = id;
      // add this one to set the credential PK:
      this.credential.setPersonId(id);
   }

}


It should work as You do not have a generator class for the Credential.id and the default strategy is assigned .

Good Luck !

_________________
JK@res


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 3:46 pm 
Newbie

Joined: Thu Dec 15, 2005 1:17 pm
Posts: 5
Thanks for your quick reply.
Once I declare the setId in my Person class, who calls it to assign the id value? or how does the Primary Key generated gets passed through hibernate to this method?
THanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 4:03 pm 
Newbie

Joined: Sun Aug 07, 2005 3:55 am
Posts: 11
Location: slovakia
well You should definitely read the documentation ... basically: Hibernate would call this method when the id for your entity is generated (based on the generator used in Your case: <generator class="increment"/>) ... so after session.save your instance is persistent and has na id and that id is assigned to Your instance calling that method ... as long as You do not change the accessor mechanism ;-))) ...

Everything is nicely explained in the reference manual:

<hibernate-mapping
default-access="field|property|ClassName"
/>

(default is property thus that method will be invoked)

... or you might specify accesors separately for properties and so on ... eg in Your case for id:

<id
access="field|property|ClassName">
</id>

(make sure this is set to property, so Your setter will be called, if You change the default).


Do not forget to rate ME if I helped ;-) !

_________________
JK@res


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 5:23 pm 
Newbie

Joined: Thu Dec 15, 2005 1:17 pm
Posts: 5
That is fantastic. That worked. You saved my day.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 4:09 pm 
Newbie

Joined: Thu Dec 15, 2005 1:17 pm
Posts: 5
The Insert Part worked fine with the solution.
On the retrieval when I do a createQuery like this
List persons = session.createQuery("from Person").list();
hibernate just hangs.
In my debug, it just prints the select query it trys to run and then exits the method.
I am suspecting that hibernate uses the same setter methods on my POJO object and when it sees that there are 2 values to be set (in my parent object) it does not know how to resolve it.
I don't know if anybody has a solution for this. If so, I will greatly appreciate any help I get.
Thanks


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