Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Change Event on Entity Graph [EclipseLink]

Can I by only using JPA somehow keep track of a whole Entity-Graphs state?

Consider having a Hierarchy with two or three classes in them with at least one Collection.

By using JPA @Post... Annotations I can only keep track of the actual entity and not its children and or Collections as it is pretty much just the Data-Base Event wrapped.

I know Hibernate can do that, but I don't want to rely on the implementation of the JPA for doing this.

I would be pretty happy if there was a way to do this with EclipseLink at least.

like image 561
Martin Braun Avatar asked Dec 03 '14 22:12

Martin Braun


1 Answers

Not sure if this is what you're searching for, but you can detect change on your entities like so:

 Account a = em.merge(account);
 final JpaEntityManager jpaEntityManager = (JpaEntityManager) em.getDelegate();
 final UnitOfWorkChangeSet changeSet = jpaEntityManager.getUnitOfWork().getCurrentChanges();
 final ObjectChangeSet accountChangeSet = changeSet.getObjectChangeSetForClone(a);
 final ObjectChangeSet userChangeSet = changeSet.getObjectChangeSetForClone(a.getUser());
 em.clear();
 //checks only for these 2 specific fields; changeSet.hasChanges() would check for all changes
 return accountChangeSet.hasChangeFor("credits") || userChangeSet.hasChangeFor("name");

Hope this somewhat helps.

like image 85
thr0wable Avatar answered Sep 28 '22 06:09

thr0wable