2017-06-13 3 views
0

J'ai 2 entité BlackListCritères de se joindre à deux tables Hibernate

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; 

et

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; 

s'il vous plaît aidez-moi. Comment créer des critères pour moi d'obtenir des données pour cette requête: 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; 
    } 

cette méthode me retourne/id et le numéro/mais j'ai besoin seul numéro

Répondre

-1

Il y a deux façons d'aborder, vous pouvez sélectionner les candidats à partir de la liste noire en utilisant hql.Or vous pouvez ajouter la jointure inverse dans votre candidat, ce qui vous permet de créer un alias et d'ajouter une restriction non nulle.

+0

pourrait-on me aider à écrire cela? –

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

i besoin de ce .. Peut-être que quelqu'un va aider