14

I was using JPA AttributeConverter with spring boot 1.2.1 RELEASE and it worked fine. But I get the following error after upgrading to spring boot 1.3.0.RELEASE

    Caused by: javax.persistence.PersistenceException: Error attempting to apply AttributeConverter
    at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$1.bind(AttributeConverterSqlTypeDescriptorAdapter.java:103)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3248)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:159)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
    ... 81 more
Caused by: java.lang.NullPointerException
    at rafito.spring.boot.domain.TimestampPersistenceConverter.convertToDatabaseColumn(TimestampPersistenceConverter.java:16)
    at rafito.spring.boot.domain.TimestampPersistenceConverter.convertToDatabaseColumn(TimestampPersistenceConverter.java:10)
    at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$1.bind(AttributeConverterSqlTypeDescriptorAdapter.java:97)

I am not setting serviceBeginTime and serviceEndTime in my POST JSON currently. But this wasn't giving me a null pointer before I upgraded spring boot version.

Converter class

@Converter
public class TimestampPersistenceConverter implements AttributeConverter<Long, Timestamp> {


    @Override
    public Timestamp convertToDatabaseColumn(Long aLong) {
        return new Timestamp(aLong);
    }

    @Override
    public Long convertToEntityAttribute(Timestamp timestamp) {
        return timestamp.getTime();
    }
}

Entity

@Column(name = "service_begin_time")
@Convert(converter = TimestampPersistenceConverter.class)
Long serviceBeginTime;

@Column(name = "service_end_time")
@Convert(converter = TimestampPersistenceConverter.class)
Long serviceEndTime;

Database fields

service_begin_time timestamp without time zone,
service_end_time timestamp without time zone,
Appy
  • 601
  • 2
  • 7
  • 12
  • and why don't you look at the stack trace of the exception, maybe there is a nested exception. Better still post it in the question so then people have information – Neil Stockton Dec 09 '15 at 08:03
  • so there's an NPE in your converter ... so perhaps fix your converter to cater for nulls. – Neil Stockton Dec 09 '15 at 15:54
  • Yes..That was my workaround `@Override public Timestamp convertToDatabaseColumn(Long aLong) { if (aLong == null) return null; return new Timestamp(aLong); }` – Appy Dec 09 '15 at 16:01
  • This could also happen if the set of named values passed to your `AttributeConverter` doesn't have a match with the original data received by the converter. I mean this is an obvious potential reason, but stating it for anyone like me who won't suspect data! – RBz Jul 09 '22 at 15:55

1 Answers1

15

Added a null check as that was the easiest.

@Override
    public Timestamp convertToDatabaseColumn(Long aLong) {
        if (aLong == null)
            return null;
        return new Timestamp(aLong);
    }
Appy
  • 601
  • 2
  • 7
  • 12