1

我有 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 。我需要一个可以与此实现一起使用的解决方案。

4

1 回答 1

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();
于 2013-08-30T12:35:00.990 回答