[英]How to use a Multiple SELECT query using JPA
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. 所以如果我有名字,我想从数据库中选择imagename和refName。
I found something here http://www.objectdb.com/java/jpa/query/jpql/select 我在这里找到了一些东西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());
}
The problem is because of this code: 问题是由于以下代码:
q.setParameter("i.name", plan.getFace()[i].name);
Take a look at your JPQL: 看一下您的JPQL:
SELECT i.name, i.refName, i.imageName FROM Items AS i
You have no parameter named i.name
. 您没有名为
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);
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.