简体   繁体   English

用于自定义查询的Hibernate映射

[英]Hibernate mapping for custom query

I have this repository method with @Query 我有@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);

And my EmployeeDetails class constructor looks like this 我的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);
    }

From my service class I am calling the above repository method with all 4 params as expected. 从我的服务类中,正在按预期使用所有4个参数调用上述存储库方法。 However when building my Application, I get an error 但是,在构建我的应用程序时,出现错误

    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]

One note here would be that the dateFormat param is not in the Employees table. 这里要注意的一点是dateFormat参数不在Employees表中。 I have passed that as an argument from my service class so I can use that value as part of the resultSet in my EmployeeDetails constructor. 我已将其作为服务类的参数传递,因此可以在EmployeeDetails构造函数中将该值用作resultSet的一部分。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

I re-edited the answer, I was able to run locally without error. 我重新编辑了答案,我能够在本地运行而不会出现错误。 I made some changes in the code. 我对代码进行了一些更改。 Please look into it, and it might fix your issue if you haven't fixed it yet. 请调查一下,如果您尚未解决,它可能会解决您的问题。 I gave the full package name in front of DTO class and added native sql is true. 我在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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM