jpa - IllegalArgumentException:您试图为预期类型为 java.math.BigDecimal 的参数标记设置类型为 java.lang.String 的值

标签 jpa jpql illegalargumentexception

当我执行以下查询时,

public int checkIfQueryExist(String query){
    List<Product> products= em.createQuery("SELECT p FROM Product p WHERE p.markup=:markup" ).
            setParameter("markup", query).getResultList(); 
    return products.size();
}

我得到以下异常:
Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter markup with expected type of class java.math.BigDecimal from query string SELECT p FROM Product p WHERE p.markup=:markup.
    at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:937)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:593)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:1)
    at enterpriseBeans.Operations.checkIfQueryExist(Operations.java:31)
    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.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    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 com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    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 com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    ... 60 more

这是怎么引起的,我该如何解决?

最佳答案

该问题隐藏在堆栈跟踪中
Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter markup with expected type of class java.math.BigDecimal from query string SELECT p FROM Product p WHERE p.markup=:markup.
尝试转换 query输入 BigDecimal在您的 checkIfQueryExist方法

关于jpa - IllegalArgumentException:您试图为预期类型为 java.math.BigDecimal 的参数标记设置类型为 java.lang.String 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959158/

相关文章:

org.hibernate.ejb.criteria.predicate.ComparisonPredicate.render 处的 java.lang.NullPointerException

java - 带有 Hibernate 的 Spring JPA : How to verify that an object was saved?

java - 为什么 JPA 对于 @ManyToOne 关系默认使用 FetchType EAGER

java - thread.setPriority(0) 给出 IllegalArgumentException

java - SessionFactory.getCurrentSession() 与 EntityManager.createEntityManager()

java - 这个简单的 JPQL 查询究竟是如何工作的?

jpa - JPQL 不断抛出 The expression is not a valid conditional expression

java - 在 Hibernate 中使用 HQL 进行查询

Android IllegalArgumentException 为 dismissDialog

android - IllegalArgumentException:文件包含路径分隔符 Android