简体   繁体   中英

Criteria from join two tables Hibernate

I have 2 entity BlackList

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;

and

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;

please help me. How create criteria for get me data for this query: 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;
    }

this method returned me /id and number/ but i need only number

  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;
    }

i need this.. Maybe someone will help

有两种方法可以解决此问题,您可以使用hql从黑名单中选择申请人,也可以在申请人中添加反向联接,从而允许您“创建别名”并添加“非空”限制。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM