使用 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) 返回一个对象时获取firstname 和albumname 行
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
?
How do I map the persons.get(0)
and get the firstname
and albumname
?
推荐答案
现在如何在persons.get(0) 返回一个对象时获取firstname 和albumname 行
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:
指定的查询结果类型通过查询的 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_expressions 用于SELECT子句,查询的结果是 Object[]
类型,并且此结果中的元素对应于按照他们的顺序SELECT 子句中的规范和in type 到每个结果类型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]);
}
请注意,通过在 FROM 子句中使用笛卡尔积和在 WHERE 子句中指定连接条件来指定内部连接,不如指定实体关系上的显式连接(使用 [LEFT [OUTER]| INNER ] JOIN
语法).请参阅规范中的整个部分 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 节连接"
这篇关于使用 JPQL 从两个表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!