-->
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.  [ 5 posts ] 
Author Message
 Post subject: Foreign key constraint fails (What hibernate update)??
PostPosted: Fri Jul 16, 2004 4:38 am 
Newbie

Joined: Fri Jul 16, 2004 4:32 am
Posts: 4
Please, help me..

I cannot solve my problem during 3 days..

So, i have 2 tables (CoffeeOrder and CoffeeOrderItem), 1:M, bidirectional.

(1) Scripts for creating tables

create table CoffeeOrder (
id int not null, -- pk
type_id int not null, --
order_date datetime not null, --
name varchar(100), --
delivery_address varchar(200) not null, --
cost double, --
primary key (id)
) type=InnoDB;

create table CoffeeOrderItem (
id int not null, -- pk
order_id int not null, --
quantity int, --
primary key (id)
) type=InnoDB;

create index COI_I on CoffeeOrderItem (
order_id asc
);

-- FK
alter table CoffeeOrderItem
add constraint COI_CO foreign key (order_id)
references CoffeeOrder (id);


(2) My mapping files

Map for Order class

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

<hibernate-mapping>
<class name="task.Order" table="CoffeeOrder" dynamic-update="true">

<id name="id" type="int"
unsaved-value="null">
<column name="id" sql-type="int"
not-null="true"/>
<generator class="assigned"/>
</id>
<bag name="children" inverse="true" cascade="all" lazy="true">
<key column="order_id"/>
<one-to-many class="task.OrderItem"/>
</bag>
<property name="type_id">
<column name="type_id" sql-type="int"
not-null="true"/>
</property>

<property name="order_date">
<column name="order_date" sql-type="datetime"
not-null="true"/>
</property>

<property name="name">
<column name="name" sql-type="varchar(100)"
not-null="false"/>
</property>

<property name="delivery_address">
<column name="delivery_address" sql-type="varchar(200)"
not-null="false"/>
</property>

<property name="cost">
<column name="cost" sql-type="double"
not-null="true"/>
</property>

</class>
</hibernate-mapping>

Map for OrderItem class

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

<hibernate-mapping>
<class name="task.OrderItem" table="CoffeeOrderItem" dynamic-update="true">

<id name="id" type="int"
unsaved-value="null">
<column name="id" sql-type="int"
not-null="true"/>
<generator class="assigned"/>
</id>

<many-to-one name="order_id" class="task.Order" column="order_id" not-null="true"/>

<property name="quantity">
<column name="quantity" sql-type="int"
not-null="true"/>
</property>
</class>
</hibernate-mapping>


(3) My Classes...

public class Order implements java.io.Serializable{
....
public List getChildren() { return children; }
public void addChild(OrderItem child){
child.setOrder_id(this);
children.add(child);
}
public void setChildren(List children) { this.children=children; }
public void setId(int id){
this.id = id;
}
.... getter(), setter()
}

public class OrderItem implements java.io.Serializable{
private int id;
private Order order_id;
private int quantity;

public OrderItem(){}
.... getter, setter();
}


(4) Error description:

Exception happen when i execute this code (when adding Order to database):

sess = sf.openSession();
Order order = new Order();
order.setId(8);
order.setType_id(8);
order.setCost(777.0);
order.setOrder_date(Calendar.getInstance());
order.setName("my name");
order.setDelivery_address("my addr");
order.setChildren(new ArrayList());
try{
sess.save(order);
sess.flush();
}
catch(Exception ex)
{System.out.println(ex);}
sess.close();

This exception (and generated SQL):

[java] Hibernate: insert into CoffeeOrder (type_id, order_date, name, deliv
ery_address, cost, id) values (?, ?, ?, ?, ?, ?)
[java] Jul 16, 2004 10:56:08 AM net.sf.hibernate.util.JDBCExceptionReporter
logExceptions
[java] WARNING: SQL Error: 1216, SQLState: S1000
[java] Jul 16, 2004 10:56:08 AM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
[java] SEVERE: General error message from server: "Cannot add or update a child row: a foreign key constraint fails"

I don't understand, what Hibernate Update?? I add only empty Order without childs..
This exception appear even if i set cascade="none"...

But i can read data from database, and exception not occured:

try{
Query query =
sess.createQuery("select item from task.Order as item");
List list = query.list();
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Order p = (Order)iterator.next();
System.out.println(p);
java.util.List chld = (java.util.List)p.getChildren();
else{
Iterator it = chld.iterator();
while(it.hasNext()){
System.out.println((OrderItem)it.next());
}
}//else
}


Help me, please.

Best thanks..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 5:05 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Looked in glance... It is strange... should work.
on http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-onetomany
wrote
Quote:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, Hibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter


As I see you use bidirectional association, and everything should be fine, you also bind your children to the parent in
Code:
public void addChild(OrderItem child){
    child.setOrder_id(this);
    children.add(child);
}


But what is wrong? ...
For test try to remove NOT NULL constraint for CoffeeOrderItem.order_id,
let it be:
Quote:
create table CoffeeOrderItem (
id int not null, -- pk
order_id int, -- NOT NULL removed
quantity int, --
primary key (id)
) type=InnoDB;


And at first try without index for CoffeeOrderItem.order_id.

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 5:15 am 
Newbie

Joined: Fri Jul 16, 2004 4:32 am
Posts: 4
I have this method in Order:
Code:
public void addChild(OrderItem child){
      child.setOrder_id(this);
      children.add(child);   
   }


And in OrderItem:
Code:
public void setOrder_id(Order order_id){
      this.order_id = order_id;
   }


Even if i add children to Order:

Code:
   OrderItem item = new OrderItem(10,10);
   order.addChild(item);
   sess.save(order);


This exception occuire..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 5:17 am 
Newbie

Joined: Fri Jul 16, 2004 4:32 am
Posts: 4
websans1 wrote:
I have this method in Order:
Code:
public void addChild(OrderItem child){
      child.setOrder_id(this);
      children.add(child);   
   }


And in OrderItem:
Code:
public void setOrder_id(Order order_id){
      this.order_id = order_id;
   }


Even if i add children to Order:

Code:
   OrderItem item = new OrderItem(10,10);
   order.addChild(item);
   sess.save(order);


This exception occuire..


I cannot allow order_id = NULL.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 5:20 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Try for test without NOT NULL constraint and look if it works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.