【问题标题】:Hibernate failing to read or write a org.joda.time.LocalDateHibernate 无法读取或写入 org.joda.time.LocalDate
【发布时间】:2016-04-13 16:55:08
【问题描述】:

我正在使用 UserType 3.2.0-GA、Hibernate 4.3.8,与 AWS (RDS) 上的现有 mysql 实例通信。

我正在尝试从 Hibernate 3.2.6 和 joda-time-hibernate 1.1 升级应用程序。

失败的属性:

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

型号:

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

尝试读取现有数据时的错误:

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)

尝试写入新数据时的错误:

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

如您所见,我正在使用 Spring 进行配置。我在春天的休眠属性是:

<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>

【问题讨论】:

  • 我也尝试将属性配置为无类型以让 usertype 做它的事情:&lt;property name="birthdate" not-null="true"/&gt; 但这似乎没有任何区别。
  • Joda + Hibernate 不能开箱即用。检查thisthis
  • 是的,这就是为什么我立即声明我们之前使用 joda-time-hibernate 1.1 进行集成,但根据您的第二个链接升级到 usertype 3.2.0。

标签: java mysql hibernate jodatime usertype


【解决方案1】:

原来我通过将类型设置为:

&lt;property name="birthdate" not-null="true" type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate"/&gt;

不幸的是,我不明白为什么这是有效的,甚至是必要的。更多解释会很棒。

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2013-10-10
    • 2018-09-03
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多