在spring hibernate查询中的日期

时间:2017-10-19 11:55:16

标签: java spring hibernate jpa jpql

我有一种从数据库中获取教育事件的方法。不知道为什么它不能运行。似乎查询仅在某些情况下失败,但无法真正找出破坏它的输入参数。我无法从日志中获取有关位置1853处语法错误的任何提示。您是否有任何想法导致问题或如何进一步调试该方法?

public List<EE> findEEs(S s, List<EM> eMs, E e) {
    TypedQuery<EE> query = entityManager.createQuery(
        "select distinct ee from EE ee left join ee.lSE lse "
            + "where ee.eM in (:em) "
            + "and (ee.scope = :publicScope "
            + "or (ee.scope = :limitedScope and (ee.hE= :e or :e in lse)) "
            + "or (ee.scope = :localScope and ee.hE = :education)) "
            + "and ee.status = :status and ee.startDate > :now "
            + "and (ee.groupSize = 0 or (ee.groupSize > 0 "
            + "  and ee.groupSize > (ee.numberOfStudents + ee.reservedSeats))) "
            + "and ee.id not in (select pee.eE.id from PEE pee "
            + "where studentStatus.s = :s and eE.eM in (:em))",
        EE.class);

    query.setParameter("s", s);
    query.setParameter("em", eMs);
    query.setParameter("publicScope", PUBLIC);
    query.setParameter("limitedScope", LIMITED);
    query.setParameter("localScope", LOCAL);
    query.setParameter("e", e);
    query.setParameter("status", CONFIRMED);
    query.setParameter("now", new DateTime());

    return query.getResultList();

日志显示:

2017-10-19 14:46:32,673 [http-nio-8090-exec-10] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42601
2017-10-19 14:46:32,674 [http-nio-8090-exec-10] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: syntax error at or near ")"
  Position: 1853
lokakuuta 19, 2017 2:46:32 IP. org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver logException
WARNING: Resolved exception caused by Handler execution: org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Data access failure

Sorry, a problem occurred while accessing the database.

org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
com.sun.proxy.$Proxy435.findUnaddedEducationEvents(Unknown Source)

1 个答案:

答案 0 :(得分:1)

来自docs

  

逗号分隔列表中必须至少有一个元素   定义IN表达式的值集。

确保传入的参数至少包含一个值。

另请查看相关的question