java 在 JPA-QL 查询中加入集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6774355/
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
Joining to a collection in a JPA-QL query
提问by rym
I'am using JPA for mapping ,I have this entity Class
我正在使用 JPA 进行映射,我有这个实体类
@Entity
@Table(name = "h_pe")
@XmlRootElement
@NamedQueries({
public class HPe implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected HPePK hPePK;
@Column(name = "PE_TIMEOUT")
private Integer peTimeout;
@Column(name = "PE_STATUS")
private Boolean peStatus;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "hPe")
private Collection<HPesp> hPespCollection;
@JoinColumn(name = "PE_ENV", referencedColumnName = "ENV_URL", insertable = false, updatable = false)
@ManyToOne(optional = false)
private HEnv hEnv;
@JoinColumn(name = "PE_PLATFORM", referencedColumnName = "PLATFORM_NAME", insertable = false, updatable = false)
@ManyToOne(optional = false)
private HPlatform hPlatform;
}
I want to write a request JPA like the following(I have wrote it with SQl ),I have tried to write it but I haven't understood how to use PE_ENV because it is the result of OneToMany relationship!
我想写一个像下面这样的请求JPA(我已经用SQl写了它),我已经尝试写了但我不明白如何使用PE_ENV,因为它是OneToMany关系的结果!
select distinct h_env.env_name,h_platform.PLATFORM_NAME
from h_env,h_platform,h_pe
where h_env.ENV_URL=h_pe.PE_ENV
and h_platform.PLATFORM_NAME=h_pe.PE_PLATFORM
and h_platform.PLATFORM_NAME='XXX';
回答by James
Select e.name, p.name
from HPe hp join hp.hPlatform p join hp.hPespCollection p
where p.name = 'xxx'
See, http://en.wikibooks.org/wiki/Java_Persistence/Querying#Common_Queries
见, http://en.wikibooks.org/wiki/Java_Persistence/Querying#Common_Queries