JPA:加入 JPQL

发布于 2024-09-19 17:49:11 字数 1206 浏览 0 评论 0原文

我以为我知道如何在 JPQL 中使用 JOIN 但显然不知道。谁能帮助我吗?

select b.fname, b.lname from Users b JOIN Groups c where c.groupName = :groupName

这给了我异常

org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query
Internal Exception: org.eclipse.persistence.internal.libraries.antlr.runtime.EarlyExitException

UsersGroups有OneToMany关系。

Users.java

@Entity
public class Users implements Serializable{

    @OneToMany(mappedBy="user", cascade=CascadeType.ALL)
    List<Groups> groups = null;
}

Groups.java

@Entity
public class Groups implements Serializable {
    @ManyToOne
    @JoinColumn(name="USERID")
    private Users user;
}

我的第二个问题是假设这个查询返回一个唯一的结果,那么如果我

String temp = (String) em.createNamedQuery("***")
    .setParameter("groupName", groupName)
    .getSingleResult();

***代表该查询上面的名字。那么 fnamelname 是在 temp 内连接在一起还是我得到一个 List 返回?

I thought I know how to use JOIN in JPQL but apparently not. Can anyone help me?

select b.fname, b.lname from Users b JOIN Groups c where c.groupName = :groupName

This give me Exception

org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing the query
Internal Exception: org.eclipse.persistence.internal.libraries.antlr.runtime.EarlyExitException

Users have a OneToMany relationship with Groups.

Users.java

@Entity
public class Users implements Serializable{

    @OneToMany(mappedBy="user", cascade=CascadeType.ALL)
    List<Groups> groups = null;
}

Groups.java

@Entity
public class Groups implements Serializable {
    @ManyToOne
    @JoinColumn(name="USERID")
    private Users user;
}

My second question is let say this query return a unique result, then if I do

String temp = (String) em.createNamedQuery("***")
    .setParameter("groupName", groupName)
    .getSingleResult();

*** represent the query name above. So does fname and lname concatenated together inside temp or I get a List<String> back?

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

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

发布评论

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

评论(1

旧时浪漫 2024-09-26 17:49:11

JPQL 中一对多关系的连接如下所示:

select b.fname, b.lname from Users b JOIN b.groups c where c.groupName = :groupName 

当在 select 子句中指定多个属性时,结果返回为 Object[]

Object[] temp = (Object[]) em.createNamedQuery("...")
    .setParameter("groupName", groupName)
    .getSingleResult(); 
String fname = (String) temp[0];
String lname = (String) temp[1];

顺便说一下,为什么您的实体以复数形式命名,这很令人困惑。如果你想使用复数形式的表名,你可以使用@Table显式指定实体的表名,这样就不会干扰保留字:

@Entity @Table(name = "Users")     
public class User implements Serializable { ... } 

Join on one-to-many relation in JPQL looks as follows:

select b.fname, b.lname from Users b JOIN b.groups c where c.groupName = :groupName 

When several properties are specified in select clause, result is returned as Object[]:

Object[] temp = (Object[]) em.createNamedQuery("...")
    .setParameter("groupName", groupName)
    .getSingleResult(); 
String fname = (String) temp[0];
String lname = (String) temp[1];

By the way, why your entities are named in plural form, it's confusing. If you want to have table names in plural, you may use @Table to specify the table name for the entity explicitly, so it doesn't interfere with reserved words:

@Entity @Table(name = "Users")     
public class User implements Serializable { ... } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文