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.