java - How to fetch the user if all subjects are passed by using hibernate criteria? -


i working on hibernate using criteria api ,i have student , subjects 2 entities there ,student have 1 many relationship subjects ,in subjects table have 1 column result ,now want write query if student passed subjects(based on result column ) want fetch user other wise don't want return user.how write criteria query this.

@entity(name="student") public class student {     @column(name="id")     private int id;      @column(name="name")     private string name;      @onetomany(mappedby = "student", cascade = cascadetype.all)     private list<subject> subject = new arraylist<subject>();   //setter getter methods }  @entity(name="subject") public class subject {     @column(name="id")     private int id;      @column(name="subject_name")     private string subjectname;      @manytoone     @joincolumn(name="student_id")     private suduent suduent;      @column(name="result)     private string result;  //setter getter methods } 

please check below sample data student :

id------name  1-------test  2------test2 

subjects

id ---- subject_name ------ result------   studnetid  1------java------------------pass---------1  2-------.net----------------fail---------1  3-------java---------------pass---------2  4--------.net------------pass ---------2 

any 1 me.

hi try answer query undertood
1. criteria can select projections, or root entity.
2. below code snippet should work retrieve students result "pass".(i assume store value in result when student passes subject)

criteria criteria = session.createcriteria(student.class, "student"); criteria.createalias("student.subject", "subject "); criteria.add(restrictions.eq("subject.result", "pass"); criteria.setresulttransformer(criteriaspecification.distinct_root_entity); 
  1. hql better , simpler you, try below hql ..
    again making assumption 'pass' result.

    select distinct student student student join student.subject subject subject.result = 'pass'


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -