Using Boolean definitions it is impossible to know when data has been deleted, if the field is defined as a time type it is possible to realize the corresponding requirement with fewer fields.
I have tried the following but I get startup errors in Spring Boot Jpa.
import jakarta.persistence.AttributeConverter;
import java.time.LocalDateTime;
public class SoftDeleteConverter implements AttributeConverter<Boolean, LocalDateTime> {
@Override
public LocalDateTime convertToDatabaseColumn(Boolean attribute) {
return attribute ? LocalDateTime.now() : null;
}
@Override
public Boolean convertToEntityAttribute(LocalDateTime dbData) {
return dbData != null;
}
}
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.NullPointerException: Cannot invoke "org.hibernate.type.descriptor.WrapperOptions.getSession()" because "wrapperOptions" is null
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802)
... 15 more
Caused by: java.lang.NullPointerException: Cannot invoke "org.hibernate.type.descriptor.WrapperOptions.getSession()" because "wrapperOptions" is null
at org.hibernate.type.descriptor.jdbc.spi.BasicJdbcLiteralFormatter.unwrap(BasicJdbcLiteralFormatter.java:33)
at org.hibernate.type.descriptor.jdbc.internal.JdbcLiteralFormatterTemporal.appendJdbcLiteral(JdbcLiteralFormatterTemporal.java:82)
at org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter.toJdbcLiteral(JdbcLiteralFormatter.java:37)
at org.hibernate.boot.model.internal.SoftDeleteHelper.resolveSoftDeleteMapping(SoftDeleteHelper.java:170)
at org.hibernate.persister.entity.AbstractEntityPersister.resolveSoftDeleteMapping(AbstractEntityPersister.java:5196)
at org.hibernate.persister.entity.AbstractEntityPersister.prepareMappingModel(AbstractEntityPersister.java:5181)
at org.hibernate.persister.entity.AbstractEntityPersister.prepareMappingModel(AbstractEntityPersister.java:5009)
at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess.execute(MappingModelCreationProcess.java:84)
at org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess.process(MappingModelCreationProcess.java:42)
at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:199)
at org.hibernate.internal.SessionFactoryImpl.initializeMappingModel(SessionFactoryImpl.java:371)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:300)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:457)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1506)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
... 19 more