2016-05-14 23 views
0

我有兩個實體:如何從衆多獲取對象一個關係,通過一些財產

@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 

如何到location.name == "Something"得到所有futurePlan實例?

+0

我正在做一個服務層。 –

+0

你是否必須使用HQL,SQL或更具體的東西? –

+0

標準將是最好的解決方案。但另一個解決方案也會很好。 –

回答

1

1.You可以很好地使用本地查詢類似下面:

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(); 
  • 也可以實現通過使用HQL以及相同的結果。

  • 你也可以使用Criteria Query。

  • 相關問題