在spring与hibernate整合时,对持久化类我才用了注解的方式。
在配置sessionfactory,通过<property name="annotatedClasses">这种方式加载持久化类时,单元测试通过。但通过<property name="packagesToScan">这种方式加载时却报错。错误信息如下:
Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: org.daily.validation.entity.EntityTest at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:797) at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2710) at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2648) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2635) at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1102) at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975) at org.daily.dao.hibernate.dao.EntityOperationImpl.getEntity(EntityOperationImpl.java:20) at org.daily.dao.hibernate.dao.EntityOperationImpl$$FastClassBySpringCGLIB$$25e0b371.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ... 40 more
其中奥妙百思不得其解,还望高手赐教!
另附上配置 文件。
<!--start 配置SessionFactory,默认为MYSQL --> <bean id="sessionFactoryMySQL" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSourceMySQL"></property> <!-- mappingResources以及hibernateProperties用hibernate.cfg.xml方式加载 --> <property name="configLocation" value="classpath:org/daily/dao/hibernate/resources/hibernate.cfg.xml" /> <!-- 注解的实体类通过此扫描的方式注入 用这方式程序报错--> <property name="packagesToScan"> <list> <value>org.daily.validation.entity.*</value> </list> </property> <!-- 指定实体类 ,用这种方式不报错--> <!-- <property name="annotatedClasses"> <list> <value>org.daily.validation.entity.EntityTest</value> </list> </property> --> </bean> <!--end 配置SessionFactory,默认为MYSQL -->
EntityTest
package org.daily.validation.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "test") public class EntityTest extends NoteEntityImpl { @Column(name = "name") private String name; @Column(name = "notes") private String note; @Column(name = "price") private Integer price; public EntityTest() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } }
<p>
</p><p>
<br>
</p>
<p></p>
<p>
<br>
</p>
<propertyname="packagesToScan">
<list>
<value>org.daily.validation.entity.</value>
</list>
</property>
用<spanstyle="font-size:13.3333px;">packagesToScan的话value标签里的“”要指明实体类,不能用*
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。