8

我正在尝试将 Hibernate 5 (5.2.11) 与 Spring ORM 一起使用。

以下教程我想出了以下配置:

春豆

<bean id="sessionFactorySettings" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.connection.driver_class">org.h2.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:h2:~/.dummy/settings</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>/hibernate.cfg.xml</value>
        </list>
    </property>
</bean>

休眠(hibernate.cfg.xml)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="entity.Settings"/>
    </session-factory>
</hibernate-configuration>

此配置导致org.hibernate.UnknownEntityTypeException: Unable to locate persister: entity.Settings.

但是,只要我移动所有

<prop key="hibernate.xxx">..</prob>

属性到hibernate.cfg.xml中,我将 Spring 配置更改为

<bean id="sessionFactorySettings" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="/hibernate.cfg.xml"/>
</bean>

一切正常。

知道我做错了什么吗?

PS:依赖:

dependencies {
    compile 'ch.qos.logback:logback-classic:1.2.3'
    compile 'org.springframework:spring-context:4.3.11.RELEASE'
    compile 'org.springframework:spring-jdbc:4.3.11.RELEASE'
    compile 'org.springframework:spring-orm:4.3.11.RELEASE'
    compile 'org.hibernate:hibernate-core:5.2.11.Final'
    compile 'org.hibernate:hibernate-java8:5.2.11.Final'
    compile 'org.apache.commons:commons-dbcp2:2.1.1'
    compile 'com.h2database:h2:1.4.196'
}
4

2 回答 2

4

根据 Spring 文档,LocalSessionFactoryBean#setMappingResources方法应该用于提供 HBM 映射文件,而不是 Hibernate 配置文件(例如hibernate.cfg.xml)。

这就是为什么它不起作用。但是,只要您使用configLocation属性,它就会起作用,因为这是提供特定于 Hibernate 的配置文件的预期方法。

现在,由于您可能使用注释,因此您根本不需要使用setMappingResources。仅当您想使用基于 XML 的 HBM 文件来提供 Hibernate 映射时才需要这样做。

你需要的是LocalSessionFactoryBean#setAnnotatedClasses而不是。或者setPackagesToScan它允许您只提供entities文件夹,并且将注册其中的所有实体类。

于 2017-09-20T05:35:25.410 回答
2

我在使用hibernate和Spring的时候一般会使用这种配置:

<bean id="hibernateSessionFactory"  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="hibernateDatasource" />
    <!-- HERE YOU HAVE TO PUT THE PACKAGE 
         WHERE YOUR ENTITY CLASS ARE LOCATED 
         (I mean classes annotated with @Entity annotation -->
    <property name="packagesToScan" value="hibernate.models" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                ${hibernate.props.db.dialect}
            </prop>
            <prop key="hibernate.show_sql">
                ${hibernate.props.db.show.sql}
            </prop>
            <prop key="hibernate.generate_statistics">
                ${hibernate.props.db.generate.statistics}
            </prop>
            <prop key="hibernate.format_sql">
                ${hibernate.props.db.format.sql}
            </prop>
            <prop key="hibernate.hbm2ddl.auto">
                ${hibernate.props.db.ddl.instr}
            </prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.props.db.use.cache}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.props.db.use.query.cache}</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
            </prop>
            <prop key="net.sf.ehcache.configurationResourceName">hibernateEhCacheCfg.xml</prop>
            <prop key="hibernate.jdbc.batch_size">${hibernate.props.db.jdbc.batch.size}</prop>
            <prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
        </props>
    </property>
</bean>

然后,我的所有属性都使用属性文件加载

我希望它有用

安杰洛

于 2017-09-19T12:52:48.097 回答