-->
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.  [ 1 post ] 
Author Message
 Post subject: foreign key mapping - Mysql innodb - constraint violation
PostPosted: Sat Apr 14, 2007 12:52 pm 
Newbie

Joined: Fri Sep 02, 2005 3:51 pm
Posts: 17
Hi all,

I am using hibernate 3 with Mysql Innodb engine.

I have a user class that references a customer table (so basically the user table has a foreign key called customer_id from the customer table).

The problem I am facing is that when I create a customer with id=0 (a customer with id=0 does not exist in the database), I get a foreign key violation exception. I understand why I am getting this error from mysql. But the requirement is that I want to create a user who does not belong to any customer (say for example the user I am creating is an admin user).

How can I add a user in the db such that the customer is set to null? The mapping for the User object is shown below (its part of a joined-class, but I am only showing whats relevant).

My question basically is that if I have an object 'A' that has a foreign key reference to another object 'B', is there no way to add a row in the table for 'A' and leaving 'B' null?

Please help!


<joined-subclass name="User" table="users">
<key column="person_id"/>

<property name="username" column="username" type="string" not-null="true" unique="true"/>
<property name="password" column="password" type="string" not-null="true"/>
<property name="enabled" column="enabled" type="boolean" not-null="true"/>
<property name="dateCreated" column="date_created" type="timestamp" not-null="true"/>

<set name="roles" lazy="false" table="user_role" cascade="save-update">
<key column="person_id" not-null="true"/>
<many-to-many class="ca.sysconet.ecourier.model.Role" column="role_id"/>
</set>


<property name="customerId" column="customer_id" type="long"/>
<many-to-one name="customer"
class="ca.sysconet.ecourier.model.Customer"
column="customer_id"
update="false"
insert="false"
lazy="false"
not-found="ignore"
/>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.