-->
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: Forein key constraint failed .
PostPosted: Mon Mar 29, 2010 7:37 am 
Newbie

Joined: Tue Mar 23, 2010 9:48 am
Posts: 8
Hi

I'm using one to many mapping with *Set*,,,, while inserting data its giving the Error like "Foreign key constraint failed"

My Mapping files are :

[*]1
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name ="com.sample.User" table="x_user">
<id name="userid" type="long" column="user_id">
<generator class="increment"/>
</id>
<property name="userName" type="string" column="user_name"/>

<set name="roles" lazy="false" cascade="all">
<key column="user_id"/>
<one-to-many class="com.sample.Role"/>
</set>
</class>
</hibernate-mapping>

[*]2


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.sample.Role" table="x_role">
<id name="roleid" type="long" column="role_id">
<generator class="increment"/>
</id>
<property name="roleName" type="string" column="role_name"/>
</class>
</hibernate-mapping>

[*]Tables structure :



CREATE TABLE x_user (user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(30) NOT NULL,
PRIMARY KEY(user_id ))ENGINE =InnoDB;


CREATE TABLE x_role (role_id INT NOT NULL AUTO_INCREMENT ,
user_id INT NOT NULL ,
role_name VARCHAR(30) NOT NULL,
PRIMARY KEY(role_id ),
CONSTRAINT `0-122` FOREIGN KEY( user_id) REFERENCES x_user (user_id) )ENGINE =InnoDB;


Please correct me where i have done the wrong and how to rectify that ?

I hopes can get quick reply.

Thanks,
Malli.


Top
 Profile  
 
 Post subject: Re: Forein key constraint failed .
PostPosted: Mon Mar 29, 2010 7:55 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is covered in the Parent/Child example in the Hibernate documentation: http://docs.jboss.org/hibernate/stable/ ... child.html

Basically, it happens because you have declared x_role.user_id column NOT NULL, but you have not mapped the corresponding column in your Role class.


Top
 Profile  
 
 Post subject: Re: Forein key constraint failed .
PostPosted: Mon Mar 29, 2010 8:13 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

you use a unidirectional one-to-many association on a foreign key,
which is an unusual case, and is not recommended
(see http://docs.jboss.org/hibernate/core/3.3/reference/en/html/associations.html#assoc-unidirectional-12m section 7.2.3 ).

Your current mapping maps the foreign key on the schema with NOT NULL
Therefore before you persists a new role instance, it already must be assigned to a user.

Solution: try to user the not-null="true" property

Code:
<set name="roles" lazy="false" cascade="all">
<key column="user_id" not-null="true"/>
<one-to-many class="com.sample.Role"/>
</set>


Or better: switch to another type of mapping which use is not discouraged.


Top
 Profile  
 
 Post subject: Re: Forein key constraint failed .
PostPosted: Mon Mar 29, 2010 8:24 am 
Newbie

Joined: Tue Mar 23, 2010 9:48 am
Posts: 8
Hi,
Thanks lot its useful for me.

Thanks
Malli.


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.