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.