異常情況:
最近在把一個項目拆分多個 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.*")
