如何通过某些属性将对象从多个关系转换为一个关系

时间:2016-05-14 14:56:04

标签: java hibernate jpa

我有两个实体:

@Entity
public class FuturePlan {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int fpId;
    private String about;
    private Date travelDate;
    @ManyToOne(fetch=FetchType.LAZY)
    private User user;
    @ManyToOne(fetch=FetchType.LAZY)
    private Location location;

    ....contructors and get/set

...和

@Entity
public class Location {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int lId;
    private String lName;
    ....contructors and get/set

如何获取futurePlan的所有location.name == "Something"个实例?

1 个答案:

答案 0 :(得分:1)

1.您可以很好地使用如下所示的原生查询:

 String q="select * from employer employer,employee where employer.id=
                    employee.employerId and employee.location='xyz'";
Query query= em.createNativeQuery(q,Object[].class);
List<Object[]> students= query.getResultList();
  1. 您也可以使用HQL获得相同的结果。

  2. 您也可以使用条件查询。