Nathan Bubna
Nathan Bubna

Reputation: 6933

Hibernate failing to read or write a org.joda.time.LocalDate

I'm using UserType 3.2.0-GA, Hibernate 4.3.8, talking to an existing mysql instance on AWS (RDS).

I'm trying to upgrade the app from Hibernate 3.2.6 and joda-time-hibernate 1.1.

The failing property:

<property name="birthdate" not-null="true" type="org.joda.time.LocalDate"/>

The model:

import org.joda.time.LocalDate;
...
public LocalDate getBirthdate() {
    return this.birthdate;
}

The error when trying to read existing data:

org.hibernate.type.SerializationException: could not deserialize (Caused by: java.io.StreamCorruptedException: invalid stream header: 31393936)
org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)
org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)
org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:155)
org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:130)
org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:44)
org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:71)
org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338)
...
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)

The error when trying to write new data:

WARN: SQL Error: 1292, SQLState: 22001
Apr 12, 2016 5:12:07 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Data truncation: Incorrect date value: '��' for column 'birthdate' at row 1
Apr 12, 2016 5:12:07 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 1292, SQLState: HY000
Apr 12, 2016 5:12:07 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: Incorrect date value: '��' for column 'birthdate' at row 1

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

As you can see, i'm using Spring to configure things. My hibernate properties in spring are:

<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
        </props>
    </property>
</bean>

Upvotes: 0

Views: 830

Answers (1)

Nathan Bubna
Nathan Bubna

Reputation: 6933

Turns out i got it to work by setting the type to:

<property name="birthdate" not-null="true" type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate"/>

Unfortunately, i don't understand why this works or is even necessary. More explanation would be great.

Upvotes: 1

Related Questions