2017-08-01 60 views
1

嗨我收到以下錯誤。Spring Hibernate Jpa錯誤:根本原因java.lang.IllegalArgumentException:未知的參數位置:1

java.lang.IllegalArgumentException: Unknown parameter position: 1] with root cause java.lang.IllegalArgumentException: Unknown parameter position: 1 at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:240) at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:503) at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372) at com.sun.proxy.$Proxy63.setParameter(Unknown Source) at edu.zipcloud.cloudstreetmarket.core.daos.HistoricalIndexRepositoryImpl.findLastHistoric(HistoricalIndexRepositoryImpl.java:37) at edu.zipcloud.cloudstreetmarket.core.daos.HistoricalIndexRepositoryImpl.findLastIntraDay(HistoricalIndexRepositoryImpl.java:31) at edu.zipcloud.cloudstreetmarket.core.services.MarketServiceImpl.getLastDayIndexActivity(MarketServiceImpl.java:41) at 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at 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(); 
    } 
} 

請幫我解決這個錯誤。如果您需要其他信息,請告訴我。

問候。

+0

您正在使用HQL?如果是,則嘗試使用'from HistoricalIndex h',其中h.index.code =:code ORDER BY h.toDate desc' 並設置參數時,使用'sqlQuery.setParameter(「code」,code);' – zombie

+0

yes Sir Sir @殭屍 – user7036414

+0

哦,親愛的。似乎忘記了查詢中的「SELECT {別名}」。另外JPA使用POSITION PARAMETERS(或NAMED PARAMETERS),而不是JDBC語法。閱讀基本的JPA文檔 –

回答

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節。查詢創建

相關問題