JPA:加入 JPQL
我以为我知道如何在 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
Users
与Groups
有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();
***
代表该查询上面的名字。那么 fname
和 lname
是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPQL 中一对多关系的连接如下所示:
当在
select
子句中指定多个属性时,结果返回为Object[]
:顺便说一下,为什么您的实体以复数形式命名,这很令人困惑。如果你想使用复数形式的表名,你可以使用@Table显式指定实体的表名,这样就不会干扰保留字:
Join on one-to-many relation in JPQL looks as follows:
When several properties are specified in
select
clause, result is returned asObject[]
: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: