0

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?

6
  • Use @ImportResource to load the xml instead of trying to convert everything. First get a working application using your existing XML files, using @ImportResource to 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. Commented Jan 26, 2021 at 12:45
  • I am already past that point. I have updated the question with new updates. Can you have a look? Commented Jan 26, 2021 at 13:24
  • Check to see if your table is correctly mapped to your entity class. javabeat.net/hibernate-querysyntaxexception/…. Commented Jan 26, 2021 at 13:30
  • The query is using the exact name of the entity. As it is described in the link you shared. Commented Jan 26, 2021 at 13:41
  • 1
    If you are following the best practices (that is an @SpringBootApplication annotated 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 the EntityManager and switch to JPA. With the current state of JPA you rarely need Hibernate and when you do you can simply unwrap to the Session if you really must. This allows you to take advantage of the JPA auto configuration as well. Commented Jan 26, 2021 at 13:50

1 Answer 1

1

With the Java based annotation configuration in Spring Boot, data access is automatically managed with :

application.propertie file:

here you define, the connection string and all other feature you want while accessing your database

@Repository interface:

an interface that helps simplified the boiler plate needed to access a database by providing native CRUD functions

@Entity class:

a class that represents your tables in memory

This link should help you.

Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to mention somethings in the Original question. So the whole project is already migrated with exactly the annotations that you recommended in your answer. 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. 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 add annotatedClasses explicitly to sessionFactory? PS: I am updating the question with these details as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.