使用 JPQL 从两个表中选择
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 SELECT 子句中包含多个 select_expressions 的查询返回一个
Object[]
(或Object[]
的List
)。来自 JPA 规范:因此,在您的情况下,您可能需要这样的内容:
请注意,通过在 FROM 子句中使用笛卡尔积和在 WHERE 子句中使用联接条件来指定内部联接,不如在实体关系上指定显式联接(使用
[LEFT [OUTER] | INNER ] JOIN
语法)。请参阅规范中的整个部分 4.4.5 连接。参考
Queries with multiple select_expressions in the SELECT clause return an
Object[]
(or aList
ofObject[]
). From the JPA specification:So in your case, you probably want something like this:
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