-->
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: fail to make persistence becuase of foreign key constraint
PostPosted: Thu Dec 16, 2004 2:46 pm 
Newbie

Joined: Wed Dec 08, 2004 10:35 pm
Posts: 7
Hibernate version:2.17c

Name and version of the database you are using:My 4.0SQL

Hello,

I have two tables:

Table Users
-------------------
int UserID (PK)
int UserTypeID Not Null (FK)
Users primaryUser;
USers orginalUser;


Table UserTypes
-------------------
int UserTypeID (PK)


Subclass of Users:(discriminator UserTypeID)
Doctors when UserTypeID = 1
Patients when UserTypeID = 2


Mapping file:

Code:
<class name="Users" table="users" discriminator-value="0">
   <id name="userID" column="UserID" type="long">
   <generator class="native" />
   </id>

#1   <discriminator column="UserTypeID"
                         type="java.lang.Integer" />
      

#2   <many-to-one name="userTypes" column="UserTypeID"
      class="com.medicaltelecom.hibernate.model.UserTypes"   
                      insert="false" update="false" not-null="true" />
   <many-to-one name="orignalUsers"
                       column="OrignalUserID" />
   <many-to-one name="primaryUsers"
                       column="PrimaryUserID" />

   <subclass name="Doctor"
      discriminator-value="1" />
   <subclass name="Patient"
      discriminator-value="2" />
</class>


Class Files:

Code:
Users{
     int UserID;
     UserTypes userTypes;
     User  primaryUsers;
     User orignalUsers;
     ...
}

Doctors extends Users{}
Patients extends Users{}

UserTypes{
     int userTypeID;
...
}


My truble is:
Because of userTypeID is a discriminator (#1) used for sub class, then in <many-to-one> (#2)of "userTypes", I have to set "insert" and "update" to be false, but the "UserTypeID" must be non-null, so how can I insert a Users record in database?

every time when I new Users() and try to make it persistence, I got exception
--Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"

Thanks a lot for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 16, 2004 3:08 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
I'm having trouble understanding what you're trying to achieve here, but it looks very strange.

But, it would seem that the problem you're encountering is that your Users class isn't abstract, and you only have a discriminator defined for the subclasses... so when you instantiate a Users object and try to save it, Hibernate won't put anything in the UserTypeID column because it's not a Doctor or Patient.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 16, 2004 3:56 pm 
Newbie

Joined: Wed Dec 08, 2004 10:35 pm
Posts: 7
Thanks for reply.

Let be clear my trouble abit clear.

The table Users has a froeign key UserTypeID from table UserTypes,

Code:
Users                                   UserTypes
------------                         -------------------
  UsersID                /------>       UserTypesID
  UserTypesID    <------/     

The mapping class Users has two sub class (Doctors and Patients), in order to handle this inheritance using table-per-class-hierarchy strategy , I have to set a discriminator (which is UserTypesID). Because the discriminator is also the foreign key from another table, in mapping file I need to set <many-to-one ... insert="false" update = "false">, which means I can not insert or update this column.

But when I initialiate a new Users object, we will also need to set the "UserTypeID", then conflicet happen.

I am not sure whether my understanding is correct, but now I can not saveOrUpdate(Users) into persistence.

Hope i make it clearer than before.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 16, 2004 4:15 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
Unfortunately that hasn't made it much clearer to me. It still seems you are trying to instantiate a Users object. Can you post the actual code?


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.