-->
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.  [ 2 posts ] 
Author Message
 Post subject: delete parent/child problem
PostPosted: Sun Dec 05, 2010 8:21 am 
Newbie

Joined: Sun Dec 05, 2010 7:56 am
Posts: 2
Hi

I've got a problem with deleting parent/child record from database, the problem is that hibernate generates
delete for parent before deleting his child and database complains

Code:
   ERROR: update or delete on table "customer" violates foreign key constraint "customer_id_fk" on table "orders"
   Detail: Key (cus_id)=(1) is still referenced from table "orders".

   ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
  org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update


the relation is configured

Customer.hbm.xml
Code:
  <set name="orderses" inverse="true" table="orders" fetch="select" cascade="all,delete-orphan">
            <key on-delete="cascade">
                <column name="cus_id" />
            </key>
            <one-to-many class="Orders" />
  </set>


Orders.hbm.xml
Code:
    <many-to-one name="customer" class="Customer" fetch="select">
            <column name="cus_id" not-null="true" />
   </many-to-one>


Service for deleting customer, where i load customer that i want to delete
Code:
   public void deleteCascade(){
      
      try{
         CustomerHome cusDao = new CustomerHome();
         
         CustomerHome.getSession().beginTransaction();
         Customer cus = (Customer)CustomerHome.getSession().load(Customer.class,Integer.valueOf("1"));      

         cusDao.delete(cus);
         
         CustomerHome.getSession().getTransaction().commit();
         CustomerHome.getSession().close();
         
         
         }catch(Exception ex){
                            System.out.println("Error1578: "+ ex.getMessage());
                        }
      
   }



Hibernate generate one delete for removing the customer, and that is the part that i am confused about.
Code:
  Hibernate: delete from testo.customer where cus_id=?


in order to remove customer hibernate must delete his orders first.

What is wrong with configuration? and does lazy loading (in my case is true) has to do any thing with it?

if yes please help guys, thanks .


Top
 Profile  
 
 Post subject: Re: delete parent/child problem
PostPosted: Sun Dec 05, 2010 8:44 am 
Newbie

Joined: Sun Dec 05, 2010 7:56 am
Posts: 2
Solved by deleteing

1.inverse="true"
2.on-delete="cascade"


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