user2691659
user2691659

Reputation:

JPA failing (java.lang.IllegalArgumentException)

I have a webservice that fails for apparently no reason. Sometimes it executes and sometimes it provides this error which i can't seem to figure out since the query actually makes sense...

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)

In the typed query,

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);

As i said sometimes it works. It fails continuously sometimes when i restart the service.

If i said the currentEmployeeID as integer and not object employee it will provide a more clear error which i understand why..(something like, i am attempting to set value of type integer for parameter.......)

Upvotes: 0

Views: 181

Answers (2)

Snorky35
Snorky35

Reputation: 425

It seems that the parameter passed to the method is of type Employees when you're waiting for an identifier. Try the following :

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

Upvotes: 1

V G
V G

Reputation: 19002

EntityManager.createQuery() expects an JPQL query (not an SQL query). Try the following:

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

Upvotes: 0

Related Questions