2017-06-13 162 views
0

我有2個實體 黑名單標準休眠

public class BlackList { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id", unique = true, nullable = false) 
    private Integer id; 

    @ManyToOne 
    @JoinColumn(name = "applicant_id", unique = true) 
    private Applicant applicant; 

public class Applicant { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id", unique = true, nullable = false) 
    private Integer id; 

    @Column(name = "number", nullable = false, unique = true) 
    private String number; 

請幫助我。如何創建標準得到我的數據的查詢:select applicant.number from black_list inner join applicant on black_list.applicant_id = applicant.id

public List<BlackList> getAll(){ 
     Session session =sessionFactory.getCurrentSession(); 
     ProjectionList projectionList = Projections.projectionList(); 
     Criteria criteria = session.createCriteria(BlackList.class); 
     projectionList.add(Projections.property("applicant")); 
     criteria.setProjection(projectionList); 
     List res = criteria.list(); 
     return res; 
    } 

此方法返回ME/ID和數字/但我只需要數

回答

-1

有兩種方法可以解決這個問題,可以選擇申請從黑名單中使用hql.Or,您可以在您的申請人中添加反向連接,允許您「創建別名」並添加「非空」限制。

+0

,我們可以幫我寫這個? –

0
public List<String> getAll(){ 
     Session session = sessionFactory.getCurrentSession(); 
     ProjectionList projectionList = Projections.projectionList(); 
     Criteria criteria = session.createCriteria(BlackList.class); 
     criteria.createCriteria("applicant", "a"); 
     projectionList.add(Projections.property("a.number")); 
     criteria.setProjection(projectionList); 
     List<String> res = criteria.list(); 
     return res; 
    } 

我需要這個..也許有人會幫