Java 使用 JPQL 从两个表中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3567438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select from two tables using JPQL
提问by Pradyut Bhattacharya
I'm using JPQL
to retrieve data. I can get data using the statement
我正在使用JPQL
检索数据。我可以使用语句获取数据
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));
}
Now I want to get the album name and the roleuser's row named firstname
现在我想获取专辑名称和角色用户的行命名 firstname
I'm using the query
我正在使用查询
persons = em.createQuery("select r.firstName , p.albumName from PhotoAlbum p ,
Roleuser r where r = p.userId and r.userID = 1").getResultList();
Now how do I get the rows firstname and albumname as the persons.get(0) is returning a object
现在我如何在persons.get(0) 返回一个对象时获取firstname 和albumname 行
by running the code :
通过运行代码:
for (i=0;i<persons.size(); i++)
{
//r = (Roleuser) persons.get(i);
System.out.println("Testing n "+ i +" " + persons.get(i));
}
I'm getting this:
我得到这个:
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
How do I map the persons.get(0)
and get the firstname
and albumname
?
我如何映射persons.get(0)
和获取firstname
和albumname
?
采纳答案by Pascal Thivent
Now how do get the rows firstname and albumname as the persons.get(0) is returning a object
现在如何在persons.get(0) 返回一个对象时获取firstname 和albumname 行
Queries with multiple select_expressionsin the SELECT clause return an Object[]
(or a List
of Object[]
). From the JPA specification:
在 SELECT 子句中具有多个select_expressions 的查询返回 an Object[]
(或 a List
of Object[]
)。从 JPA 规范:
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.
The result type of the SELECT clause is defined by the the result types of the select_expressionscontained in it. When multiple select_expressionsare 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.
4.8.1 SELECT 子句的结果类型
查询的 SELECT 子句指定的查询结果类型是实体抽象模式类型、状态字段类型、聚合函数的结果、构造操作的结果或这些的某个序列。
SELECT 子句的结果类型由其中包含的select_expressions的结果类型定义。当SELECT子句中使用多个 select_expressions时,查询的结果是type
Object[]
,这个结果中的元素按照它们在SELECT子句中的说明顺序和type对应每个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]);
}
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 Joinsin the specification.
请注意,通过在 FROM 子句中使用笛卡尔积和在 WHERE 子句中使用连接条件来指定内部连接,不如指定实体关系上的显式连接(使用[LEFT [OUTER] | INNER ] JOIN
语法)。请参阅规范中的整个部分4.4.5 连接。
References
参考
- 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"
- JPA 1.0 规范
- 第 4.8.1 节“SELECT 子句的结果类型”
- 第 4.8.2 节“SELECT 子句中的构造函数表达式”
- 第 4.4.5 节“连接”