私は2つのエンティティを持っていAddressEntity
ますCompanyEntity
.
住所エンティティ
@Entity
@Table(name = "address")
public class AddressEntity implements Serializable {
private static final long serialVersionUID = 6149442393833549397L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name = "city", nullable = false)
private String city;
@Column(name = "post")
private String post;
@Column(name = "street", nullable = false)
private String street;
@Column(name = "building_nr", nullable = false)
private Integer buildingNr;
@Column(name = "flat_nr")
private Integer flatNr;
// setters and getters
}
会社の実体
@Entity
@Table(name = "company")
public class CompanyEntity implements Serializable {
private static final long serialVersionUID = 3635072833730133590L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@OneToOne
@Cascade(CascadeType.ALL)
private AddressEntity address = new AddressEntity();
@OneToMany(mappedBy = "company")
@Cascade(CascadeType.ALL)
private Set<DescriptionEntity> descriptions = new HashSet<DescriptionEntity>();
@OneToMany(mappedBy = "company")
@Cascade(CascadeType.ALL)
private List<EmployeeEntity> employees = new ArrayList<EmployeeEntity>();
}
select all addresses which are used by companies
このSQLクエリで好きになりたいのですSELECT a.city, c.name FROM address a INNER JOIN company c ON c.address_id=a.id;
が、HQLクエリを使用します(WHEREではなくJOINを使用したい)。どうすればいいですか?会社テーブルではなく、住所テーブルを使用して住所を選択したい。select c.address.city, c.name from CompanyEntity c
このような会社のテーブルを使用するか、 WHERE を使用してアドレスを選択できることを知っていますselect a.city, c.name from CompanyEntity c, AddressEntity a WHERE c.address.id=a.id
。