天天看點

No property * found for type entity!,SpringData JPA 中自定義存儲庫的問題,總結

問題出現,最近在學​​QueryDSL​​時,起初搭好環境後,啟動i項目一直報錯,No property findOffEmpCust found for type CustomersEntity!

No property * found for type entity!,SpringData JPA 中自定義存儲庫的問題,總結

代碼示例

@Repository
public interface CustomerRepo extends JpaRepository<CustomersEntity,Long>, JpaSpecificationExecutor<CustomersEntity>, CustomerRepoCustom {
}      
public interface CustomerRepoCustom {

    // 自定義傳回
    public List<OffEmpCustVo> findOffEmpCust();

}      
@PersistenceContext
private EntityManager em;

@Override
public List<OffEmpCustVo> findOffEmpCust() {
    JPAQuery<OffEmpCustVo> query = new JPAQuery<>(em);

    QCustomersEntity customers = QCustomersEntity.customersEntity;
    QOfficesEntity office = QOfficesEntity.officesEntity;
    QEmployeesEntity employee = QEmployeesEntity.employeesEntity;

    List<Tuple> list = query.select(
            office.officeCode,
            office.country.as("offCountry"),
            office.state.as("offState"),
            employee.employeeNumber.as("emploNumber"),
            employee.email.as("emploEmail"),
            customers.customerNumber.as("custNumber"),
            customers.state.as("custState")).from(office).leftJoin(employee).on(office.officeCode.eq(employee.officeCode)).leftJoin(customers).on(employee.employeeNumber.eq(customers.salesRepEmployeeNumber)).fetchResults().getResults();

    List<OffEmpCustVo> voList = list.stream().map(r -> JSONUtil.toBean(JSONUtil.toJsonStr(r), OffEmpCustVo.class)).collect(Collectors.toList());

    return voList;

    // select off.officeCode , off.country, off.state , em.employeeNumber, em.email, cu.customerNumber , cu.state  from offices off left outer join employees em on (off.officeCode=em.officeCode) left outer join customers cu on (em.employeeNumber=cu.salesRepEmployeeNumber)
}      

注意:上圖是改後的目錄結構,之前我的命名不是這樣的,才會出錯,總結一下,當需要自定義存儲庫,一定要和本身的存儲庫,名稱一定要對應,還有自定義存儲庫的實作類命名一定要規範以Impl字尾,前以自定義存儲庫的名稱字首。