迁移到Hibernate 5.2给出错误引起:org.hibernate.HibernateException:无法确定类的类型:java.util.Collection

时间:2016-06-07 10:13:20

标签: spring-mvc spring-data-jpa jpa-2.1 hibernate-5.x

我正在使用spring项目。这是我的POM片段

.....
<!-- Spring Data module for JPA repositories. -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>${spring-data-jpa.version}</version>  //1.10.1.RELEASE
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>${javax.persistence.version}</version>  //2.1.1
    <scope>compile</scope>
</dependency>

<!-- Hibernate entity manager with JPA 2 support. 
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate-entitymanager.version}</version>  // 5.1.0.Final
    <scope>runtime</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>${javassist.version}</version>    // 3.20.0-GA
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>${hibernate-validator.version}</version>   // 5.2.4.Final
    <scope>runtime</scope>
    <exclusions>
        <exclusion>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator-annotation-processor</artifactId>
    <version>${hibernate-validator.version}</version>  // 5.2.4.Final
    <scope>compile</scope>
    <optional>true</optional>
</dependency>
.....

我有一个Spring Data JPA存储库

public interface LmsRoleLmsFeatureRepository extends CrudRepository<LmsRoleLmsFeature, Long> {
    List<LmsRoleLmsFeature> findByLmsRoleIdIn(Collection<Long> ids);
}

这是我的弹簧配置

@Configuration
@EnableTransactionManagement(mode = AdviceMode.PROXY, proxyTargetClass = false, order = 2)
@EnableJpaRepositories(
    basePackages = "a.b.c.repositories",
    entityManagerFactoryRef = "entityManagerFactory",
    transactionManagerRef = "transactionManager"
)
@Import({DatabasePropertiesConfig.class})
public class PersistenceConfig {

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_MAX_FETCH_DEPTH = "hibernate.max_fetch_depth";
    private static final String PROPERTY_NAME_HIBERNATE_JDBC_FETCH_SIZE = "hibernate.jdbc.fetch_size";
    private static final String PROPERTY_NAME_HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";

    private static final String[] ENTITYMANAGER_PACKAGES_TO_SCAN = {"a.b.c.entities", "a.b.c.converters"};

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("driverClassName"));
        dataSource.setUrl(env.getProperty("url"));
        dataSource.setUsername(env.getProperty("username"));
        dataSource.setPassword(env.getProperty("password"));
        return dataSource;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
         JpaTransactionManager transactionManager = new JpaTransactionManager();
         transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
         return transactionManager;
    }

    @Bean
    public HibernateJpaVendorAdapter vendorAdaptor() {
         HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
         return vendorAdapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setJpaVendorAdapter(vendorAdaptor());
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);   
        entityManagerFactoryBean.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
        entityManagerFactoryBean.setValidationMode(ValidationMode.NONE);
        entityManagerFactoryBean.setJpaProperties(jpaHibernateProperties());

        return entityManagerFactoryBean;
    }

    private Properties jpaHibernateProperties() {
        Properties properties = new Properties();
         properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
         properties.put(PROPERTY_NAME_HIBERNATE_MAX_FETCH_DEPTH, env.getProperty(PROPERTY_NAME_HIBERNATE_MAX_FETCH_DEPTH));
         properties.put(PROPERTY_NAME_HIBERNATE_JDBC_FETCH_SIZE, env.getProperty(PROPERTY_NAME_HIBERNATE_JDBC_FETCH_SIZE));
         properties.put(PROPERTY_NAME_HIBERNATE_JDBC_BATCH_SIZE, env.getProperty(PROPERTY_NAME_HIBERNATE_JDBC_BATCH_SIZE));

         return properties;       
    }

}

此代码工作正常。当我运行我的应用程序时,Spring应用程序上下文正确加载但是当我将我的应用程序更改为hibernate 5.2时。当我从Hibernate ORM 5.2 release读到 hibernate-entitymanager模块不再存在时。它的功能已整合到hibernate-core 中。所以我把我的POM改成了这个

....
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate-core.version}</version>   //5.2.0.Final
    <scope>runtime</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Hibernate entity manager with JPA 2 support. 
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate-entitymanager.version}</version>
    <scope>runtime</scope>
            <exclusions>
                ...
            </exclusions>
        </dependency>
-->

.....

请参阅注释掉我的hibernate-entitymanager依赖项,并仅包含hibernate-core依赖项。但是现在当我运行我的应用程序时,我收到以下错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lmsRoleLmsFeatureRepository': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Could not determine a type for class: java.util.Collection
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
.....
.....
Caused by: org.hibernate.HibernateException: Could not determine a type for class: java.util.Collection
at org.hibernate.internal.SessionFactoryImpl.resolveParameterBindType(SessionFactoryImpl.java:1023) ~[hibernate-core-5.2.0.Final.jar:5.2.0.Final]
at org.hibernate.query.criteria.internal.CriteriaBuilderImpl.parameter(CriteriaBuilderImpl.java:588) ~[hibernate-core-5.2.0.Final.jar:5.2.0.Final]
at org.springframework.data.jpa.repository.query.ParameterMetadataProvider.next(ParameterMetadataProvider.java:161) ~[spring-data-jpa-1.10.1.RELEASE.jar:?]
at org.springframework.data.jpa.repository.query.ParameterMetadataProvider.next(ParameterMetadataProvider.java:138) ~[spring-data-jpa-1.10.1.RELEASE.jar:?]

如果我从Collection更改为List。然后我得到List的相同错误。我也试过了

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.0.Final</version>
</dependency>

但同样的错误。为什么我收到此错误。我做错了吗?

谢谢&amp;此致

Basit Mahmood Ahmed

1 个答案:

答案 0 :(得分:1)

如上所述here in spring boot forums,请使用5.2.1.Final Hibernate版本,它应解决此问题。

我可以使用5.2.0在spring boot中复制你的问题。改为5.2.1.Final为我工作。