I am wondering if it's really useful to observe every change to entity attributes, even if they won't eventually get persisted. So don't you think that Entity Listeners and Callbacks woudn't be sufficient for you?They support CDI since JPA 2.1 and offer plenty of callbacks which you can observe
- @PrePersist
- @PreRemove
- @PostPersist
- @PostRemove
- @PreUpdate
- @PostUpdate
- @PostLoad
So you will get
@EntityListeners(class=Audit.class)
@Entity
public class MyUniqueObject implements Serializable {}
public class Audit {
@Inject
private Logger log;
@Inject
private Event<MyUniqueObjectEvent> events;
}
Now you can observe the lifecycle of your entity - also it's better that you have separated your model and its auditing, you don't have to mess up with setters and getters (which is confusing) to achieve logging. Also note that you can also define Default Entity Listeners for every entity you have.