With Hibernate, when I have an entity property that must not be null, I cat enforce in a fail-fast manner in the property setter
@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = checkNotNull(name, "name");
}
While this might be considered superfluous (a DB NOT NULL constraint exists anyway), I find this valuable, as – as I said – provides fail-fast validation.
Now, if I want to have the property @Audited then during reading audit records for delete, Envers fails with
org.hibernate.PropertyAccessException: Exception occurred inside setter of com.syncron.bpp.security.entity.Role.name
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:88)
at org.hibernate.envers.entities.mapper.SinglePropertyMapper.mapToEntityFromMap(SinglePropertyMapper.java:96)
at org.hibernate.envers.entities.mapper.MultiPropertyMapper.mapToEntityFromMap(MultiPropertyMapper.java:137)
at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:109)
at org.hibernate.envers.query.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:134)
at org.hibernate.envers.query.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:106)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
... 51 more
Caused by: java.lang.NullPointerException: name
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:208)
at com.example.MyEntity.setName(Role.java:126)
... 56 more
This might be logical, since there is no "name" stored in AUD table, but I've never told Envers to call my setter in this case. Why would it call it if it knows the data is all nulls?
|