开发者社区> 问答> 正文

hibernate 注解实体类,使用 <property name="p?报错

在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>

展开
收起
爱吃鱼的程序员 2020-06-09 16:24:43 748 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    <propertyname="packagesToScan">
          <list>
            <value>org.daily.validation.entity.</value>
          </list>

        </property>

    用<spanstyle="font-size:13.3333px;">packagesToScan的话value标签里的“”要指明实体类,不能用*

    2020-06-09 16:25:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载