异常情况:
最近在把一个项目拆分多个 module 的时候数据库查询遇到这个异常:org.hibernate.hql.internal.ast.QuerySyntaxException: Identification is not mapped
JPA 查询方法如下:
public Identification findIdentificationByWxId(String wxId) { JPAQueryBase query = new JPAQuery(entityManager).from(qIdentification); query.where(qIdentification.wxId.eq(wxId)); return (Identification) query.fetchOne(); }
异常信息如下:
{ "timestamp": 1548844710242, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.dao.InvalidDataAccessApiUsageException", "message": "org.hibernate.hql.internal.ast.QuerySyntaxException: Identification is not mapped [select identification\nfrom Identification identification\nwhere identification.wxId = ?1];
nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Identification is not mapped [select identification\nfrom Identification identification\nwhere identification.wxId = ?1]", "path": "/user/getUserInfo" }
解决方法:
在springboot 启动类上加上如下注解便可:
@EntityScan(basePackages = ("cn.xxx.*"))
当一个项目拆分成多个 module 的时候,由于 应用层和实体层已经隔离,所以在应用层启动应用的时候需要扫描 实体。当然如果应用层需要其他 module 中的配置类的话,也是需要添加扫描配置,扫描到其他 module 中的配置类。如:
@SpringBootApplication(scanBasePackages ="cn.xxx.*")