我有3个表,每个表都映射到一个实体。实体是这样的:
@Entity
@Table(name = "person")
public class Person implements Serializable {
private int id;
//other fields
}
@Entity
@Table(name = "phone")
public class Phone implements Serializable {
private int id;
private Long price;
@ManyToOne
@JoinColumn(name = "personId")
private Person person;
@ManyToOne
@JoinColumn(name = "manufacturerId")
private Manufacturer manufacturer;
//other fields
}
@Entity
@Table(name = "manufacturer")
public class Manufacturer implements Serializable {
private int id;
private String name;
//other fields
}
我想要做的是创建一个方法,该方法将返回具有指定制造商的电话的人员列表,其价格在指定范围内。
编辑:我的dao类实现了EntityJpaDao。我需要一个适用于此实现的解决方案。
答案 0 :(得分:1)
以下查询将返回具有手机价格范围的三星移动用户。
Criteria criteria = session.createCriteria(Phone.class, "phone");
criteria.createAlias("phone.person", "person")
criteria.add(Restrictions.between("phone.price", minPrice, maxPrice));
criteria.createAlias("phone.manufacturer","manufacturer");
criteria.add(Restrictions.eq("manufacturer.name", Samsung));
criteria.setProjection(Projections.property("person"));
List<Person> persons = criteria.list();