-->
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: Collections Dirty Checking Blues
PostPosted: Wed May 06, 2009 12:28 pm 
Newbie

Joined: Thu Dec 16, 2004 2:19 pm
Posts: 12
Given these two entities:

Code:
@Entity
class Category {
    @Id
    int id;
}

@Entity
class Ham {
    @Id
    int id;

    @ManyToMany(targetEntity=Category.class, fetch=FetchType.LAZY)
    @JoinTable(
        joinColumns={ @JoinColumn(name="hamId") }
        inverseJoinColumns={ @JoinColumn(name="categoryId") }
    )
    Collection<Category> categories = new ArrayList<Category>();

    @PostUpdate
    void postUpdate() {
        // get stashed dirty properties and do something with them
        // (see MyFlushListener below)
    }
}


Doing this operation:

Code:
Ham ham = entityManager.find(Ham.class, 123)   ;
Category cat = entityManager.find(Category.class, 456)   ;
ham.categories.add(cat);
ham = entityManager.merge(ham);


The database is updated correctly.

We have a requirement to capture property changes, hence a custom FlushEntityEventListener:

Code:
class MyFlushListener extends DefaultFlushEntityEventListener {
    @Override
    protected void dirtyCheck(FlushEntityEvent event) throws HibernateException {
        super.dirtyCheck(event);
        int[] dirtyPropertiesIdx = event.getDirtyProperties();
        // stash the dirty properties for @PostUpdate (see Ham class above)
    }
}


I encounter 3 "issues" which I hope someone can shed some light on:

1. event.getDirtyProperties() does not return "categories" as dirty,
even though categories (PersistentBag) is marked as dirty

2. @PostUpdate does not fire
There is an open issue: http://opensource.atlassian.com/project ... se/EJB-318
But I may not be able to wait for the fix.

3. in dirtyCheck(), entry.getLoadedState() which is suppose to contain the "before" value, categories is already in the final state.
i.e. I am expecting the loaded state of categories to be empty but it already contains Category456.

Anyone has any insight to help overcome these "issues" ?

Thank you.


Top
 Profile  
 
 Post subject: Re: Collections Dirty Checking Blues
PostPosted: Wed Jan 02, 2013 5:21 pm 
Newbie

Joined: Wed Jan 02, 2013 5:14 pm
Posts: 2
I too have the same problem,..
Before every flush i will check my Bag address is equal to flushing bag address.
Ur code didn't work becz Ham is never dirty, moreover its many-to-many so, non of the Entity interceptors will work, unless, the relation itself is turned to entity.
Alternative, use 3 way relation instead of many-to-many (Not @DB level, only @Mapping level).


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.