-->
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: Why does hibernate delete this entity during dirty checking?
PostPosted: Wed Mar 30, 2011 7:29 am 
Newbie

Joined: Wed Mar 30, 2011 6:59 am
Posts: 3
In pseudo I have a foo method that does as follows:

Code:
foo() {
     Domain object = retrievalMethod();
}

Domain retrievalMethod() {
   //begin transaction here
   Domain object = session.get(Domain.class, 1);
   //commit transaction here
}


My domain object contains some collections of other domain objects for which relationships are mapped in my hibernate mapping file.

When the transaction commits, dirty checking is performed as you would expect (remember nothing has actually been done apart from load retrieve a persistent object) so I would expect nothing to happen. However when I turn on hibernate logging I am getting a debug output saying a collection from my domain object has been dereferenced. Hibernate then proceeds to delete the objects with this query using an SQL statement. Then following this, hibernate then proceeds to re-add the very object that it has deleted using another SQL statement.

Could anybody shed some light on what could be causing this sort of behavior? I have trapsed through the actual hibernate source but to no avail. From what I can see in AbstractFlushingEventListener in the flushentities method hibernate iterates through every persistant object to perform dirty checking, schedule updates etc. In the case of my domain object it iterates through each of it's collections and marks them as reachable, but in the case of the actual entity which is a part of my collection, it is never setting the reachable flag to true (infact, I cannot even see a check to see if it is reachable when you are referencing a standalone object). So the flag is then set to false which triggers the delete update statement. This behaviour seems wrong to me, so I am guessing that I am missing something. Any help appreciated.


Top
 Profile  
 
 Post subject: Re: Why does hibernate delete this entity during dirty checking?
PostPosted: Wed Mar 30, 2011 9:34 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
One possible cause is that your code is not returning the exact same collection instance from the getter method as Hibernate passed in the setter method. This is briefly mentioned in the reference documentation (21.5.4 One shot delete):

Quote:
Fortunately, you can force this behavior (i.e. the second strategy) at any time by discarding (i.e. dereferencing) the original collection and returning a newly instantiated collection with all the current elements.


Top
 Profile  
 
 Post subject: Re: Why does hibernate delete this entity during dirty checking?
PostPosted: Wed Mar 30, 2011 9:51 am 
Newbie

Joined: Wed Mar 30, 2011 6:59 am
Posts: 3
Well this is slightly embarassing, I have found the problem in the setter method for my collection which has been written by somebody else. It's always something stupid that you don't notice. In pseudo I have:

Code:
    public void setDomain(Set<Domain> domains) {
        SortedSet<Domain> sortedDomains = new TreeSet<GroupType>(new GroupTypeComparator());

        for (Domain domain : domains) {
            sortedDomains.add(domain);
        }

        this.domains = sortedDomains;
    }


Obviously this is not the correct way to sort this collection, and this method should be left a simple setter. Thanks nordborg!


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.