用于自定义查询的Hibernate映射

时间:2018-09-05 17:04:44

标签: java hibernate

我在@Query中有这个存储库方法

    @Query(" SELECT new EmployeeDetails(m.accountId, m.employeeId, " +
                " DATE_FORMAT(m.eventTime,:sqlDateFormat) as eventDate," +
                " :dateFormat)                       " +
                " FROM Employee m " +
                " WHERE m.eventTime BETWEEN :startDate AND :endDate " +
                "AND m.accountId IN (:accountIdList)" +
                " GROUP BY DATE_FORMAT(m.eventTime,:sqlDateFormat),dateFormat,m.employeeId,m.accountId")
    @Transactional
    List<EmployeeDetails> findAllData(@Param("startDate") Date startDate,
   @Param("endDate") Date endDate,
   @Param("accountIdList")Set<Long> accountIdList,
   @Param("dateFormat")String sqlDateFormat),
   @Param("dateFormat")String dateFormat);

我的EmployeeDetails类构造函数看起来像这样

public EmployeeDetails(long accountId, long employeeId,String date,
                             String dateFormat) {

        this.accountId = accountId;
        this.employeeId = employeeId;
        this.startDate = getDate(date,dateFormat); //I am formatting the string date as per the dateFormat
        this.endDate = getDate(date,dateFormat);
    }

从服务类中,我正在调用上述存储库方法 所有4个参数均符合预期。 但是,在构建我的应用程序时,出现错误

    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [EmployeeDetails]. Expected arguments are: long, long, java.lang.String [  SELECT new EmployeeDetails(m.accountId, m.employeeId,                                                                                         DATE_FORMAT(m.eventTime,:sqlDateFormat) as eventDate,                                                             :dateFormat) FROM MockEmployees m  WHERE m.eventTime BETWEEN :startDate AND :endDate AND m.accountId IN (:accountIdList) GROUP BY DATE_FORMAT(m.eventTime,:sqlDateFormat),dateFormat,m.employeeId, m.accountId]
  at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:268) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
  at sun.reflect.GeneratedMethodAccessor108.invoke(Unknown Source) ~[?:?]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60]
  at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60]

这里需要注意的是,雇员表中没有dateFormat参数。我已将其作为服务类的参数传递,因此可以在EmployeeDetails构造函数中将该值用作resultSet的一部分。

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

我重新编辑了答案,我能够在本地运行而不会出现错误。我对代码进行了一些更改。请调查一下,如果您尚未解决,它可能会解决您的问题。我在DTO类的前面给出了完整的软件包名称,并添加了本地sql。

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  @Query(value = " SELECT new com.my.package.fullname.model.EmployeeDetails(m.accountId, m" +
      ".employeeId, " +
      " DATE_FORMAT(m.eventTime,:sqlDateFormat) as eventDate," +
      " :dateFormat)                       " +
      " FROM Employee m " +
      " WHERE m.eventTime BETWEEN :startDate AND :endDate " +
      "AND m.accountId IN (:accountIdList)" +
      " GROUP BY DATE_FORMAT(m.eventTime,:sqlDateFormat),dateFormat,m.employeeId,m.accountId", nativeQuery = true)
  @Transactional
  List findAllData(
      @Param("startDate") Date startDate,
      @Param("endDate") Date endDate,
      @Param("accountIdList") Set accountIdList,
      @Param("dateFormat") String sqlDateFormat,
      @Param("dateFormat") String dateFormat);
}