Hibernate ClassCastException org.hibernate.type.SerializableType无法强制转换为org.hibernate.type.VersionType

时间:2015-09-01 15:03:16

标签: java hibernate jpa jodatime h2

当我尝试运行单元测试时出现奇怪的异常。这是堆栈跟踪:

javax.persistence.PersistenceException: [PersistenceUnit: testPU] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
     ... more
Caused by: org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:174)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:401)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
    ... 39 more
Caused by: java.lang.ClassCastException: org.hibernate.type.SerializableType cannot be cast to org.hibernate.type.VersionType
    at org.hibernate.tuple.PropertyFactory.buildVersionProperty(PropertyFactory.java:181)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:218)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:520)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:148)
    at sun.reflect.GeneratedConstructorAccessor29.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
    ... 43 more

以下是我正在使用的库和框架:

  1. JPA 2.0
  2. org.hibernate作为:休眠-的EntityManager:4.3.5.Final
  3. com.h2database:H2:1.4.188
  4. 的junit:junit的:4.11
  5. Intellij IDE(我刚刚在这里运行测试)
  6. org.hibernate作为:休眠-envers:4.3.5.Final
  7. 好吧,我打赌任何看过这个的人都希望看到persistense.xml文件,所以这里是:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
        <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
    

    这里有很多课程,所以我将它们删除以保持可读性

            <properties>
                <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
                <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;MODE=PostgreSQL;INIT=create schema if not exists test;DB_CLOSE_DELAY=-1" />
                <property name="javax.persistence.jdbc.user" value="sa" />
                <property name="javax.persistence.jdbc.password" value="" />
                <property name="hbm2ddl.auto" value="create" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
                <property name="hibernate.show_sql" value="false"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    单元测试是基本的。它只是通过值找到记录。这是代码:

    @Test
      public void testFindByNaturalKeyReturnsBusinessUnit() {
        final BusinessUnit businessUnit = mock(BusinessUnit.class);
        assertThat(businessUnitDao.findByNaturalKey(businessUnit.getNaturalKey()).getId(), is(businessUnit.getId()));
      }
    

    最后,实体真的很复杂。我不确定发布代码是否会有所帮助。我想在实体中存在一个错误的关系,所以我正在研究它。

1 个答案:

答案 0 :(得分:0)

好的,有一个答案。实体类在某些字段中使用了JodaTime。这要求persistence.xml文件设置一些其他属性。

JodaTime字段的示例:

@javax.persistence.Column(name = "active_date")
private org.joda.time.DateTime activeDate;

最后,修复了persistence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

这里有很多课程,所以我将它们剪掉以保持可读性

        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;MODE=PostgreSQL;INIT=create schema if not exists test;DB_CLOSE_DELAY=-1" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hbm2ddl.auto" value="create" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="jadira.usertype.autoRegisterUserTypes" value="true"/>
            <property name="jadira.usertype.databaseZone" value="jvm"/>
            <property name="jadira.usertype.javaZone" value="jvm"/>
        </properties>
    </persistence-unit>
</persistence>