Org.hibernate.UnknownEntityTypeException: Cannot find persister: entity.Settings

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

Following the instructions, I came up with the 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 org.hibernate.UnknownEntityTypeException: Unable to locate persister: entity.Settings .

However, as soon as I move everything

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

in hibernate.cfg.xml and I am changing the Spring configuration to

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

everything is working fine.

Any idea what I'm doing 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' } 
+7
spring spring-orm hibernate
source share
2 answers

I usually use this 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> 

Then all my properties are loaded using the properties file

I hope this is helpful

Angelo

+2
source share

According to the Spring docs, the LocalSessionFactoryBean # setMappingResources method should be used to provide HBM mapping files, not a hibernation configuration file (for example, hibernate.cfg.xml ).

That is why it does not work. However, once you use the configLocation property, it works because it is the intended method for providing a Hibernate-dependent configuration file.

Now, since you are probably using annotations, you don't need to use setMappingResources . This is only necessary if you want to use XML-based HBM files to provide Hibernate mappings.

Instead, you need LocalSessionFactoryBean # setAnnotatedClasses . Or setPackagesToScan , which allows you to specify only the entities folder, and all entity classes inside will be registered.

For more information, check out my book, Java's High Performance Durability, GitHub Repository .

+2
source share

All Articles