简体   繁体   English

如何在 Spring 引导中配置 Hibernate AnnotatedClasses

[英]How to configure Hibernate AnnotatedClasses in Spring Boot

I am currently moving an old Spring project to Spring boot.我目前正在将旧的 Spring 项目移动到 Spring 引导。 In the old project we were initialising datasource and session factory in xml context explicitly.在旧项目中,我们在 xml 上下文中显式初始化数据源和 session 工厂。 But after moving to Spring Boot, all is happening implicitly through Spring boot and application.properties.但是在移动到 Spring 引导之后,一切都通过 Spring 引导和 application.properties 隐式发生。

My question is that, with no explicit creation/initialisation of datasource and sessionFactory , how can I add annotatedClasses to the hibernateSessionFactory ?我的问题是,在没有明确创建/初始化数据源和sessionFactory的情况下,如何将annotatedClasses添加到hibernateSessionFactory Can I do it through application.properties ?我可以通过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 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.问题只出现在我们正在创建查询的 Dao 中。 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. Dao 用 Repository 和 Transactional 注释,而 entity (TemplateImpl) 用 Entity 注释。

Do I still need to explicitly add annotatedClasses to the sessionFactory?我还需要显式地将 annotatedClasses 添加到 sessionFactory 吗? If not then why am I getting this error?如果不是,那么为什么我会收到此错误?

With the Java based annotation configuration in Spring Boot , data access is automatically managed with:Spring Boot中使用基于 Java 的注释配置,数据访问通过以下方式自动管理:

application.propertie file: application.propertie文件:

here you define, the connection string and all other feature you want while accessing your database在这里您定义连接字符串和访问数据库时所需的所有其他功能

@Repository interface: @Repository接口:

an interface that helps simplified the boiler plate needed to access a database by providing native CRUD functions通过提供本机 CRUD 功能帮助简化访问数据库所需的样板的界面

@Entity class: @ @Entity

a class that represents your tables in memory一个 class 代表您在 memory 中的表

This link should help you.这个链接应该可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM