org.hibernate.UnknownEntityTypeException: Unable to locate persister: entity.Settings
Asked Answered
P

2

8

I am trying to use Hibernate 5 (5.2.11) together with Spring ORM.

Following tutorials I came up with following configuration:

Spring Bean

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

This configuration leads to a org.hibernate.UnknownEntityTypeException: Unable to locate persister: entity.Settings.

However, as soon as I move all

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

properties into hibernate.cfg.xml and I change the Spring configuration to

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

everything works fine.

Any idea what I do wrong?

PS: Dependencies:

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'
}
Precipitant answered 16/9, 2017 at 15:35 Comment(2)
I never saw the configuration you wrote. I always used or the hibernate cfg file or the spring hibernate configuration. Did you try to put the entity class list inside the spring configuration?Archegonium
I thought as well that the entity class should be inside the spring configuration. But how?Precipitant
S
4

According to Spring docs, the LocalSessionFactoryBean#setMappingResources method should be used for providing HBM mapping files, not the Hibernate configuration file (e.g. hibernate.cfg.xml).

That's why it does not work. However, as soon as you use configLocation property, it works because that's the intended method for providing the Hibernate-specific configuration file.

Now, since you probably use annotations, you don't need to use setMappingResources at all. That's only needed if you want to use the XML_based HBM files to provide the Hibernate mappings.

What you need is LocalSessionFactoryBean#setAnnotatedClasses instead. Or setPackagesToScan which allows you to give just the entities folder and all entity classes inside will be registered.

Sawn answered 20/9, 2017 at 5:35 Comment(2)
I do use annotations but I thought I still need to register the classes somewhere? If I do not use any configuration for hibernate it won't work as well ...Precipitant
Use setAnnotatedClasses as I explained in my answer.Sawn
A
2

I usually use this kind of configuration when I use hibernate and 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>

All my properties are, then, loaded by using a property file

I hope it's useful

Angelo

Archegonium answered 19/9, 2017 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.