org.hibernate.MappingException(未知实体:com.app.persistence.Test)

赋料扬雄敌,诗看子建亲。这篇文章主要讲述org.hibernate.MappingException:未知实体:com.app.persistence.Test相关的知识,希望能为你提供帮助。
我正在运行一个Spring Boot项目,我想使用Hibernate和HQL查询。
我收到以下错误:

Exception in Dao org.hibernate.MappingException: Unknown entity: com.app.persistence.Test at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:783) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100) at org.hibernate.jpa.event.internal.core.JpaSaveEventListener.saveWithGeneratedId(JpaSaveEventListener.java:56) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666) at com.thp.clinic.allergiesConditions.dao.AllergiesDaoImpl.check(AllergiesDaoImpl.java:38) at com.thp.clinic.allergiesConditions.dao.AllergiesDaoImpl$$FastClassBySpringCGLIB$$6b8d942f.invoke(< generated> ) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) at com.thp.clinic.allergiesConditions.dao.AllergiesDaoImpl$$EnhancerBySpringCGLIB$$b9677149.check(< generated> ) at com.thp.clinic.allergiesConditions.service.AllergiesServiceImpl.check(AllergiesServiceImpl.java:23) at com.thp.clinic.allergiesConditions.service.AllergiesServiceImpl$$FastClassBySpringCGLIB$$7070308f.invoke(< generated> ) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) at com.thp.clinic.allergiesConditions.service.AllergiesServiceImpl$$EnhancerBySpringCGLIB$$1fddf4a.check(< generated> ) at com.thp.clinic.allergiesConditions.controller.AllergiesController.check(AllergiesController.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)

我的Model类如下:
package com.app.persistence; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="test") public class Test { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="test_id") private long testId; @Column(name="name") private String name; @Column(name="phone") private String phone; //getters and setters }

我的DAO如下:
@Autowired SessionFactory factory; @Override public String check(Test test) { Session session = null; try{ session = factory.openSession(); session.save(test); session.flush(); return "done"; } catch(Exception e) { System.out.println("Exception in Dao"); e.printStackTrace(); throw e; }

据我所知,它无法与模型类进行映射。但我无法弄清楚应该在哪里添加映射(如果需要)。
我已经在配置类中包含了@ComponentScan(basePackages = { "com.app" })
有人可以帮我解决一下吗?
答案【org.hibernate.MappingException(未知实体:com.app.persistence.Test)】添加@EntityScan(basePackages = { "youentitypackage" })@ComponentScan(basePackages = { "com.app" })。这将查找您的实体类。

    推荐阅读