-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unwanted delete after update parent/childs
PostPosted: Mon Jan 19, 2009 8:35 am 
Newbie

Joined: Mon Jan 19, 2009 6:16 am
Posts: 4
Hello,

I have changed a lot in my mappings lately, to make Hibernate cascade the delete to the RegistrationEntries and HourEntries when a Registration gets deleted.

I got that to work now, but it presented another problem.

When I try to update a Registration, it deletes the Registration afterwards? Im pretty sure I'm not deleting it manually in my code so its proberly something in my mappings.

Any of u have an idea what the problem is?

Thanks very much in advance,
Joost Pastoor

Hibernate version: 3.3.1.GA
Mapping documents:
Registration
Code:
<hibernate-mapping>
    <class name="nl.ncim.timetracking.model.Registration" table="registration">
        <id name="registrationID" column="registration_id" unsaved-value="0">
            <generator class="native"/>
        </id>
        ...
      <list name="registrationEntries" cascade="all,delete-orphan" lazy="false" inverse="true">
         <key column="registration" on-delete="cascade"/>
              <index column="registrationentry_id"/>         
         <one-to-many class="nl.ncim.timetracking.model.RegistrationEntry"/>
      </list>
      
      ....      
    </class>
</hibernate-mapping>


RegistrationEntry
Code:
<hibernate-mapping>
    <class name="nl.ncim.timetracking.model.RegistrationEntry" table="registration_entry">
        <id name="registrationEntryID" column="registrationentry_id" unsaved-value="0">
            <generator class="native"/>
        </id>
       
        ...

         <many-to-one name="registration" foreign-key="RegistrationFK" [color=red]cascade="all"[/color] class="nl.ncim.timetracking.model.Registration" column="registration" update="false" not-null="true"/>

      ...
      
      <list name="hourEntries" cascade="all,delete-orphan" inverse="true" lazy="false">
         <key column="registration_entry" on-delete="cascade"/>
              <index column="hourentry_id"/>         
         <one-to-many class="nl.ncim.timetracking.model.HourEntry"  />
      </list>       
      
    </class>
</hibernate-mapping>


HourEntry
Code:
<hibernate-mapping>
    <class name="nl.ncim.timetracking.model.HourEntry" table="hour_entry">
       
        <id name="hourEntryID" column="hourentry_id">
            <generator class="native"/>
        </id>     

       ...

         <many-to-one name="registrationEntry" class="nl.ncim.timetracking.model.RegistrationEntry"
         column="registration_entry" foreign-key="RegistrationEntryFK" [color=red]cascade="all"[/color] not-null="true"/>
     </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
session.clear();
registration = (Registration) session.merge(registration);


Full stack trace of any exception that occurs:
None.

Name and version of the database you are using:
MySQL client version: 5.0.22

The generated SQL (show_sql=true):
Code:
... inserts of all the components in the Registration
Hibernate: insert into hour_entry (hour_type, amount, registration_entry) values (?, ?, ?)
Hibernate: insert into hour_entry (hour_type, amount, registration_entry) values (?, ?, ?)
Hibernate: insert into hour_entry (hour_type, amount, registration_entry) values (?, ?, ?)
Hibernate: insert into hour_entry (hour_type, amount, registration_entry) values (?, ?, ?)
Registration saved or updated.
Hibernate: select registrati0_.registration_id as registra1_2_0_, registrati0_.create_timestamp as create2_2_0_, registrati0_.about_month as about3_2_0_, registrati0_.signed_employee as signed4_2_0_, registrati0_.signed_customer as signed5_2_0_, registrati0_.signed_administration as signed6_2_0_, registrati0_.user_id as user7_2_0_, registrati0_.project as project2_0_, registrati0_.customer as customer2_0_ from registration registrati0_ where registrati0_.registration_id=?

.... lots of selects

Hibernate: select registrati0_.registration_entry as registra7_1_, registrati0_.change_id as change1_1_, registrati0_.change_id as change1_4_0_, registrati0_.changed_field as changed2_4_0_, registrati0_.old_value as old3_4_0_, registrati0_.new_value as new4_4_0_, registrati0_.change_timestamp as change5_4_0_, registrati0_.user_id as user6_4_0_, registrati0_.registration_entry as registra7_4_0_ from registration_change registrati0_ where registrati0_.registration_entry=?
Hibernate: delete from hour_entry where hourentry_id=?
Hibernate: delete from hour_entry where hourentry_id=?
Hibernate: delete from hour_entry where hourentry_id=?
Hibernate: delete from hour_entry where hourentry_id=?
Hibernate: delete from hour_entry where hourentry_id=?
Hibernate: delete from registration_entry where registrationentry_id=?


Debug level Hibernate log excerpt:
Don't know


Last edited by JoostPastoor on Mon Jan 19, 2009 9:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 9:24 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
<many-to-one ... cascade="all" ...>


You have this in both the RegistrationEntry->Registration and HourEntry->RegistrationEntry mappings. It seems a bit weird since I get the impression that these are parent->child relations and what you are saying with cascade="all" is that if a child is deleted the parent should also be deleted. And if the parent is deleted all other children also has to be deleted.

It doesn't explain why Hibernate starts to delete things, but may explain why more things than expected are being deleted.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 9:42 am 
Newbie

Joined: Mon Jan 19, 2009 6:16 am
Posts: 4
Thanks for your quick response.

I changed the mappings from RegistrationEntry and HourEntry and removed both cascade="all" on the childsides.

Now when I update an Registration:
- The Registration stays (hooray)
- Its childs the RegistrationEntries are gone
- Childs of RegistrationEntries, the HourEntries are gone also

So it seems like a step in the good direction, but did'nt fix all yet.


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