rym rym - 1 year ago 87
Java Question

Joining to a collection in a JPA-QL query

I'am using JPA for mapping ,I have this entity Class

@Table(name = "h_pe")

public class HPe implements Serializable {
private static final long serialVersionUID = 1L;
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!

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';

Answer Source
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download