Spring Hibernate Jpa错误:根本原因java.lang.IllegalArgumentException:未知参数位置:1

时间:2017-08-01 11:25:36

标签: java spring hibernate jpa

嗨,我收到了以下错误。

  

java.lang.IllegalArgumentException:未知参数位置:1]   根本原因java.lang.IllegalArgumentException:未知参数   位置:1点   org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:240)     在   org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:503)     在   org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:104)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.orm.jpa.SharedEntityManagerCreator $ DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372)     在com.sun.proxy。$ Proxy63.setParameter(未知来源)at   edu.zipcloud.cloudstreetmarket.core.daos.HistoricalIndexRepositoryImpl.findLastHistoric(HistoricalIndexRepositoryImpl.java:37)     在   edu.zipcloud.cloudstreetmarket.core.daos.HistoricalIndexRepositoryImpl.findLastIntraDay(HistoricalIndexRepositoryImpl.java:31)     在   edu.zipcloud.cloudstreetmarket.core.services.MarketServiceImpl.getLastDayIndexActivity(MarketServiceImpl.java:41)     在   edu.zipcloud.cloudstreetmarket.portal.controllers.DefaultController.fallBack(DefaultController.java:26)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)     在   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)     在   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)     在   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     在   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:622)at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

内部方法findLastHistoric(HistoricalIndexRepositoryImpl.java:37)

类:

@Repository
public class HistoricalIndexRepositoryImpl implements HistoricalIndexRepository{

    @PersistenceContext 
    private EntityManager em;

    @Override
    public Iterable<HistoricalIndex> findIntraDay(String code, Date of) {
        TypedQuery<HistoricalIndex> sqlQuery = em.createQuery("from HistoricalIndex h where h.index.code = ? and h.fromDate >= ? and h.toDate <= ? ORDER BY h.toDate asc", HistoricalIndex.class);
        sqlQuery.setParameter(1, code);
        sqlQuery.setParameter(2, DateUtil.getStartOfDay(of));
        sqlQuery.setParameter(3, DateUtil.getEndOfDay(of));
        return sqlQuery.getResultList();
    }

    @Override
    public Iterable<HistoricalIndex> findLastIntraDay(String code) {
        return findIntraDay(code, findLastHistoric(code).getToDate());
    }

    @Override
    public HistoricalIndex findLastHistoric(String code){
        TypedQuery<HistoricalIndex> sqlQuery = em.createQuery("from HistoricalIndex h where h.index.code = ? ORDER BY h.toDate desc", HistoricalIndex.class);
        sqlQuery.setParameter(1, code);
        return sqlQuery.setMaxResults(1).getSingleResult();
    }
}

请帮我解决此错误。如果您需要其他信息,请与我们联系。

问候。

1 个答案:

答案 0 :(得分:3)

这个怎么样?

    TypedQuery<HistoricalIndex> sqlQuery = em.createQuery("from HistoricalIndex h where h.index.code = ?1 ORDER BY h.toDate desc", HistoricalIndex.class);

检查here第5.3.2节。查询创建