1
votes

Hi I am getting following error.

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)

inside method findLastHistoric(HistoricalIndexRepositoryImpl.java:37)

class:

@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();
    }
}

Please help me to resolve this error. If you need other information, let me know.

Regards.

1
you are using HQL? If yes try to use from HistoricalIndex h where h.index.code = :code ORDER BY h.toDate desc and while setting the parameter, use sqlQuery.setParameter("code", code);Prabhat
yes Sir @zombieuser7036414
Oh dear. seems to have forgotten the "SELECT {alias}" from queries. Also JPA uses POSITIONAL PARAMETERS (or NAMED PARAMETERS), not JDBC syntax. Read basic JPA docsNeil Stockton

1 Answers

3
votes

How about this?

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

Check here section 5.3.2. Query creation