使用JPQL从两个表中选择 [英] select from two tables using JPQL
问题描述
我正在使用JPQL
检索数据.我可以使用语句获取数据
I'm using JPQL
to retrieve data. I can get data using the statement
List persons = null;
persons = em.createQuery("select p.albumName from PhotoAlbum p , Roleuser r
where r = p.userId and r.userID = 1");
现在我可以使用以下名称获取相册名称:
Now I can get the album names using this:
int i=0;
for (i=0;i<persons.size(); i++)
{
System.out.println("Testing n "+ i +" " + persons.get(0));
}
现在,我想获取相册名称和角色用户的名为firstname
Now I want to get the album name and the roleuser's row named firstname
我正在使用查询
persons = em.createQuery("select r.firstName , p.albumName from PhotoAlbum p ,
Roleuser r where r = p.userId and r.userID = 1").getResultList();
现在,当persons.get(0)返回一个对象时,如何获取行的名字和相册名称
Now how do I get the rows firstname and albumname as the persons.get(0) is returning a object
通过运行代码:
for (i=0;i<persons.size(); i++)
{
//r = (Roleuser) persons.get(i);
System.out.println("Testing n "+ i +" " + persons.get(i));
}
我得到这个:
Testing n 0 [Ljava.lang.Object;@4edb4077
INFO: Testing n 1 [Ljava.lang.Object;@1c656d13
INFO: Testing n 2 [Ljava.lang.Object;@46dc08f5
INFO: Testing n 3 [Ljava.lang.Object;@654c0a43
如何映射persons.get(0)
并获取firstname
和albumname
?
推荐答案
现在,当persons.get(0)返回一个对象时,如何获取行的名字和相册名称
Now how do get the rows firstname and albumname as the persons.get(0) is returning a object
SELECT子句中具有多个 select_expressions 的
查询返回Object[]
(或Object[]
的List
).根据JPA规范:
Queries with multiple select_expressions in the SELECT clause return an Object[]
(or a List
of Object[]
). From the JPA specification:
4.8.1 SELECT子句的结果类型
指定的查询结果的类型 通过查询的SELECT子句是 实体抽象架构类型, 状态字段类型, 聚合函数,a的结果 施工作业,或一些 这些顺序.
4.8.1 Result Type of the SELECT Clause
The type of the query result specified by the SELECT clause of a query is an entity abstract schema type, a state-field type, the result of an aggregate function, the result of a construction operation, or some sequence of these.
SELECT子句的结果类型
由...的结果类型定义
其中包含的 select_expressions
它.当多个
在查询子句的SELECT子句中使用 select_expressions
的类型为Object[]
,并且
此结果中的元素对应于
按他们的顺序
SELECT子句中的规范和
在类型上与每个结果类型
select_expressions .
The result type of the SELECT clause
is defined by the the result types of
the select_expressions contained in
it. When multiple
select_expressions are used in the SELECT clause, the result of the query
is of type Object[]
, and the
elements in this result correspond in
order to the order of their
specification in the SELECT clause and
in type to the result types of each of
the select_expressions.
因此,在您的情况下,您可能想要这样的东西:
So in your case, you probably want something like this:
for (i=0;i<persons.size(); i++) {
//r = (Roleuser) persons.get(i);
System.out.println("Testing n " + i + " " + persons.get(i)[0] + ", " +
persons.get(i)[1]);
}
请注意,与在实体关系上使用显式联接(使用[LEFT [OUTER] | INNER ] JOIN
语法)相比,在FROM子句中使用笛卡尔乘积和WHERE子句中的联接条件指定内部联接的典型性较低.请参阅规范中的整个章节 4.4.5加入.
Note that specifying an inner join by the use of a cartesian product in the FROM clause and a join condition in the WHERE clause is less typical than specifying an explicit join over entity relationships (using the [LEFT [OUTER] | INNER ] JOIN
syntax). See the whole section 4.4.5 Joins in the specification.
- JPA 1.0规范
- 第4.8.1节"SELECT子句的结果类型"
- 第4.8.2节"SELECT子句中的构造函数表达式"
- 第4.4.5节加入"
- JPA 1.0 Specification
- Section 4.8.1 "Result Type of the SELECT Clause"
- Section 4.8.2 "Constructor Expressions in the SELECT Clause"
- Section 4.4.5 "Joins"
这篇关于使用JPQL从两个表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!