Reputation: 1044
I am looking to select multiple items in my DB based on the primary key.
so if I have the name I want to select imagename and refName from the DB.
I found something here http://www.objectdb.com/java/jpa/query/jpql/select
TypedQuery<Object[]> query = em.createQuery(
"SELECT c.name, c.capital.name FROM Country AS c", Object[].class);
List<Object[]> results = query.getResultList();
for (Object[] result : results) {
System.out.println("Country: " + result[0] + ", Capital: " + result[1]);
}
Which doesn't work, and someone else seemed to think the code was awfully wrong so I am curious how exactly I will do this?
Thanks all!
Caused by: java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of i.name that does not exist in the query string SELECT i.name, i.refName, i.imageName FROM Items AS i.
at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:928) at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:928)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:593)
at uploader.AdminControl.fileCreate(AdminControl.java:745)
at uploader.AdminControl.upload(AdminControl.java:660)
... 57 more
..
TypedQuery<Object[]> q = em2.createQuery("SELECT i.name, i.refName, i.imageName FROM Items AS i",Object[].class);
q.setParameter("name", plan.getFace()[i].name);
System.out.println(q);
List<Object[]> results = q.getResultList();
for (Object[] result : results)
{
bw.write(result[1].toString());
bw.write(result[2].toString());
System.out.println("result:" + result[1].toString() + "and" + result[2].toString());
}
Upvotes: 1
Views: 2393
Reputation: 1744
The problem is param name
TypedQuery<Object[]> q = em2.createQuery("SELECT i.name, i.refName, i.imageName FROM Items WHERE i.name = :someparam AS i",Object[].class);
q.setParameter("someparam", plan.getFace()[i].name);
Upvotes: 0
Reputation: 1912
The problem is because of this code:
q.setParameter("i.name", plan.getFace()[i].name);
Take a look at your JPQL:
SELECT i.name, i.refName, i.imageName FROM Items AS i
You have no parameter named i.name
. You should create one that will receive the parameter. Something like
SELECT i.name, i.refName, i.imageName FROM Items AS i where i.name = :parameterName
And do pass the value:
q.setParameter("parameterName", plan.getFace()[i].name);
Upvotes: 2