关于hibernate的异常Caused by: org.hibernate.hql.ast.QuerySyntaxException: Table is not mapped错误分析

最近新入职一家公司,用的是2.X版本的hibernate,由于长时间没用这个技术了有些生疏了,所以用起来有些不太熟练,导致了下面一些列错误,经过排查希望能给小伙伴掉坑里的一些帮助。
错误:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Cltest is not mapped [select cl from Cltest cl where id=:id]
这个错误是因为hibernate找不到实体类的映射对象导致的。那么我们需要排查几点,
NO.1 hibernate使用的是hql语句,并非sql。虽然两者很相近但还是有不同之处,我们使用hibernate的entityManager.createQuery(sql, 实体.class)来进行查找,这里的sql中我们不能使用select * from 表名,而是应该使用select st(别名) from Student(实体类名) as st(别名) 这里一定不能写表名,这里是实体类名不是表名.
发现问题后更改依旧不可以

NO.2.我们继续尝试其他办法,由于我们hibernate被spring进行管理,所以需要在spring 的xml文件中配置相应的映射文件
但由于我的是使用spring 注解方式进行配置,但我的@Entity也已经放到了类上面
python
import org.hibernate.annotations.Entity;

import com.mysql.jdbc.Blob;
@Entity
@Table(name=“cl_test”)
@SequenceGenerator(name=“sequenceGenerator”,sequenceName=“SEQ”)
public class Cltest extends com.neusoft.holo.core.entity.BaseEntity implements Serializable{

依然报错,最后才发现这个依赖注入的@Entity的包并非是hibernate的包而是要使用
import javax.persistence.Table这个包才能使用,否则就会报异常,好久不使用hibernate了被这种初级问题卡了一天也是够丢人的。没什么难度希望提醒广大程序友们不要粗心仔细看代码才是。如果有兴趣可以加我威信好友一起交流技术问题
89066560

猜你喜欢

转载自blog.csdn.net/weixin_42243829/article/details/82182707
今日推荐