JPA失败(java.lang.IllegalArgumentException)

时间:2014-05-20 23:41:40

标签: java jpa

我有一个因为没有理由而失败的网络服务。有时它会执行,有时会提供这个错误,我似乎无法弄清楚,因为查询实际上是有道理的......

java.lang.IllegalArgumentException: You have attempted to set a value of type class org.models.Employees for parameter currentEmployeeID with expected type of class org.models.Employees from query string SELECT b.carID FROM Cars b WHERE b.currentEmployeeID = :currentEmployeeID .
    at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:932)

在键入的查询中,

Employees emp = new Employees(1)

TypedQuery<Integer> query = em.createQuery("SELECT b.carID FROM Cars b WHERE b.currentEmployeeID = :currentEmployeeID", Integer.class);
            query.setParameter("currentEmployeeID",emp);

正如我所说,它有时会起作用。当我重新启动服务时,它有时会连续失败。

如果我说currentEmployeeID为整数而不是对象员工,它将提供更明确的错误,我理解为什么..(类似的,我试图为参数.......设置类型为整数的值)

2 个答案:

答案 0 :(得分:1)

当您等待标识符时,似乎传递给该方法的参数是Employees类型。请尝试以下方法:

Employees emp = new Employees(1)

TypedQuery<Integer> query = em.createQuery("SELECT b.carID FROM Cars b WHERE b.currentEmployeeID = :currentEmployeeID", Integer.class);
            query.setParameter("currentEmployeeID",emp.getId());

答案 1 :(得分:0)

EntityManager.createQuery()需要JPQL查询(不是SQL查询)。 请尝试以下方法:

em.createQuery("SELECT b.carID FROM Cars b INNER JOIN b.currentEmployee e WHERE e.id = :currentEmployeeID", Integer.class);