How to define the field type for @SoftDelete, e.g. using LocalDateTime

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

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.

I checked the latest hibernate 7.0.0 related code yesterday and this issue has been resolved in the new version.

Currently 7.0.0 is not officially released, I need to wait for a while.

If you can provide a reproducer, we can look into it and fix it for 6.6 as well.

I can wait for the new release, SoftDelete support for setting field definitions has been marked as a new feature for 7.0.0.

7.0.0 on @SoftDelete adding additional fields to set the database type.

It’s ok that you can wait, but there might be other people which could benefit from this being fixed on ORM 6.6. I can’t force you to provide a reproducer, but doing so would potentially help others.