views:

46

answers:

2

Hi All,

I am having a designation table with d_name as primary key which i am using in user table as foreign key reference .I am using hbm for mapping in designation hbm i have id defined as d_name mapped to database column .I am getting a error saying

"integrity constraint violation(user_designation_fk) parent key not found. "

Where am i going wrong /this error is coming while i am tring to add a user selecting a designation reading from designation table.

user hbm is as below

<hibernate-mapping> 
  <class name="com.User" table="USER">                            
    <id column="USER_ID" name="id" > 
      <generator class="assigned">
      </generator> 
    </id> 
    <property column="d_name" name="dName"/>

 </class> 
</hibernate-mapping> 

designation hbm is as below

<hibernate-mapping> 
  <class name="com.Designation" table="Designation"> 
    <id column="d_name" name="dName" > 
      <generator class="assigned"> 

      </generator> 
    </id> 

 </class> 
</hibernate-mapping> 

and code to save is

Session session = HibernateUtil.getSession(); 
Transaction tx = session.beginTransaction(); 
session.save(user); 
tx.commit();
A: 

Hard to give you the exact problem without seeing any code nor your mappings but did you "set" the designation on the user before to save it?

Pascal Thivent
ya i have set the designation for the user,whehter this causes a problem of having d_name as not null in designation and as null in user ?
sarah
@sarah please show your mappings and the code where you're trying to add a new user.
Pascal Thivent
A: 

Hello Sarah,

you say the property "dname" of com.User references the id of type com.designation. I suppose you have modeled it like this in the database and the column d_name in table user is a foreign key to d_name in table designation.

This means you cannot persist a user object, if you do not also persist the designation object that it references. Also if the constrained check cannot be deffered to the end of transaction as in the case of mysql, it is important to insert the referenced object first. Maybe there is even a not null constraint on that foreign key.

In any case you should declare this association also between your objects, so hibernate knows about it and can do the insert statements in correct order. I.e. in type com.user you should not use the property element but rather something like:

<hibernate-mapping>
  <class name="com.User" table="USER">                            
    <id column="USER_ID" name="id" > 
      <generator class="assigned"></generator> 
    </id>
    <many-to-one
      name="dName"
      column="d_name" />
  </class> 
</hibernate-mapping>

Please have a look at chapter 7 of the hibernate core documentation.

Regards David

David