JPQL
select p.name, d.street from Person p join Address d on d = p.address;
TO 基準から
コンストラクター付きのオプション:
まず、エンティティ @OneToOne/@OneToMany などの関係をマークします。@JoinColumn ie を使用します。
public class Person {
private Integer id;
private String name;
@JoinColumn(name="id", referencedColumnName = "id")
private Address address;
}
後で結果タイプのクラスを作成します
public class Result(){
String name;
String street;
/* you must have constructor here for the multi select later */
public Result(String name,String street) {
this.name = name;
thi.street = street
}
}
次に実行します(emはエンティティマネージャーです)
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<PlaceSearch> query = cb.createQuery(PlaceSearch.class);
Root<Person> personRoot = query.from(Person .class);
Join<Person, Address> address= personRoot .join(Person_.address);
/ * without meta model
Join<Person, Address> address= personRoot .join(person.get("address"));
*/
/* we know Result has a construct of (String,String) . this is important for the multi select here */
query.multiselect(person.get(Person_.name),address.get(Address_.street);
List<Result> result = em.
createQuery(query).getResultList();