I got problem with my application. I want to use @PreUpdate & @PrePersist to calculate some fields before updating, or creating entry. This methods lokks like this:
@PreUpdate
public void recount(){
if (applications != null){
this.appAssigned = getAssignmentCount();
this.appWaiting = getOutstandingCount();
}
}
@PrePersist
public void count(){
if (applications != null){
this.appAssigned = getAssignmentCount();
this.appWaiting = getOutstandingCount();
}
}
Whole app compiles without any errors or warnings, but when I update an entry, those methods are not invoked. I tried to debug problem, and only think I can tell is that those methods, are never invoked. No information in logs, nothing.
Anyone got any idea?
EDIT:
This entity has some basic fields, and few collection fetched lazy. This method has one job - before updating entity I need to recalculate some thing depending on those lazily fetched collections. Of course, I fetch those collections, change something, set changed collection, and then want to merge. @Pre methods are not invoked, but entity merges without any problems, all collections are changed, no errors.
FUN FACT!! I tired manually to recalculate before merging, so I added line:
myEntity.setAppAssigned(myEntity.getAssignmentCount());
entityMenager.merge(myEntity);
AND THEN @Pre methods were fired O.O I tried messing with hashcode, and equal methods, I tried merging and persisting entity, nothing works, except of manually setting one field, then methods are fired. Can anyone understands this??
EDIT 2
here is method I use to change collections:
public void doStuff(Long id, Long appId){
Entity entity = entityMenager.findAndFetchCollections(id);
Set<Foo> appList = entity .getApplications();
Foo foo = new foo();
//here I find Foo I want to change, and remove it from collection
Iterator<Foo> iterator = appList.iterator();
while (iterator.hasNext()){
foo = iterator.next();
if (foo.getId().equals(appId)){
iterator.remove();
break;
}
}
//do some stuff with found foo and add it back to collection, then set collection back to entity
foo.setFooDate(new Date());
foo.setFooStatus("PENDING");
foo.setFooIndex(fooCount + 1);
appList.add(foo);
entity.setFoo(appList);
entityMenager.merge(entity);
}
in this case methods are not invoked. If I add before merging li:
entity.setFooWaiting(entity.getFooCount());
then methods are invoked. I tried also with EntityListener but it works same way as methods in Entity class.