加入JPA-QL查询中的集合

时间:2011-07-21 10:02:14

标签: java jpa

我正在使用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;

}

我想写一个请求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';

1 个答案:

答案 0 :(得分:5)

Select e.name, p.name
from HPe hp join hp.hPlatform p join hp.hPespCollection p
where p.name = 'xxx'

请参阅, http://en.wikibooks.org/wiki/Java_Persistence/Querying#Common_Queries