使用 JPQL 从两个表中选择

发布于 2024-09-15 19:26:50 字数 1171 浏览 3 评论 0原文

我正在使用 JPQL 来检索数据。我可以使用以下语句获取数据

List persons = null;
persons = em.createQuery("select p.albumName from PhotoAlbum p , Roleuser r 
where r = p.userId and r.userID = 1");

现在我可以使用以下命令获取相册名称:

int i=0;
for (i=0;i<persons.size(); i++)
{   
     System.out.println("Testing n "+ i +" " +  persons.get(0));
}

现在我想获取相册名称和名为 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)

通过运行代码返回一个对象:

 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) 并获取 名字专辑名

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));
}

Now I want to get the album name and the roleuser's row named 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-09-22 19:26:50

现在,当persons.get(0)返回一个对象时,如何获取行firstname和albumname

在 SELECT 子句中包含多个 select_expressions 的查询返回一个 Object[] (或Object[]List)。来自 JPA 规范:

4.8.1 SELECT 子句的结果类型

指定查询结果的类型
查询的 SELECT 子句是
实体抽象模式类型,a
状态字段类型,结果
聚合函数,结果
施工作业,或一些
这些的顺序。

SELECT 子句的结果类型
由结果类型定义
包含在的 select_expressions
它。当多个
select_expressions用在SELECT子句中,查询的结果
Object[] 类型,并且
该结果中的元素对应于
按照他们的顺序
SELECT 子句中的规范和
类型为每个的结果类型
选择表达式

因此,在您的情况下,您可能需要这样的内容:

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 连接

参考

  • JPA 1.0 规范
    • 第 4.8.1 节“SELECT 子句的结果类型”
    • 第 4.8.2 节“SELECT 子句中的构造函数表达式”
    • 第 4.4.5 节“连接”

Now how do get the rows firstname and albumname as the persons.get(0) is returning a object

Queries with multiple select_expressions in the SELECT clause return an Object[] (or a List of Object[]). From the JPA specification:

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_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]);
}

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.

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文