I am currently moving an old Spring project to Spring boot. In the old project we were initialising datasource and session factory in xml context explicitly. But after moving to Spring Boot, all is happening implicitly through Spring boot and application.properties.
My question is that, with no explicit creation/initialisation of datasource and sessionFactory, how can I add annotatedClasses to the hibernateSessionFactory? Can I do it through application.properties?
Here is how the previous context looked like:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.example.myEntity</value>
</list>
</property>
We are using following versions:
Spring: 5.1.15.RELEASE, Spring-Boot: 2.1.14.RELEASE, Hibernate: 5.4.15.Final
Quick Clarification:
So the whole project is already migrated with proper annotations. The problem comes in just one occurrence where in a Dao we are creating a query. The error is that the entity referenced in that query is not mapped.
Query creation looks as follows:
getSession().createQuery("from TemplateImpl t");
The error I get is:
org.hibernate.hql.internal.ast.QuerySyntaxException: TemplateImpl is not mapped.
The Dao is annotated with Repository and Transactional and entity (TemplateImpl) is annotated with Entity.
Do I still need to explicitly add annotatedClasses to the sessionFactory? If not then why am I getting this error?
@ImportResourceto load the xml instead of trying to convert everything. First get a working application using your existing XML files, using@ImportResourceto load them. Then start to move things to java based configuration and/or use the Spring Boot autoconfiguration. Again baby steps do 1 bean at a time.@SpringBootApplicationannotated class in a top level package covering all other packages) your entities should be picked up. If that isn't the case you might need to add an@EntityScan. Also you shouldn't be using Hibnernate directly (preferably) but rather theEntityManagerand switch to JPA. With the current state of JPA you rarely need Hibernate and when you do you can simplyunwrapto theSessionif you really must. This allows you to take advantage of the JPA auto configuration as well.