org.hibernate.hql.internal.ast.QuerySyntaxException: XXX is not mapped

異常狀況:

最近在把一個項目拆分多個 module 的時候數據庫查詢遇到這個異常:org.hibernate.hql.internal.ast.QuerySyntaxException: Identification is not mappedjava

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.*")
相關文章
相關標籤/搜索