1
votes

I have following classes-tables with onetomany relationships

People

 @Entity 
 public class People{ 
     @Id @Column private Integer id;
     @Column(name="FNAME")  private String fName;   
     @Column(name="LNAME")  private String lName;   
     @OnetoMany(targetEntity=Registration.class, mappedBy="pId", cascade.....)  
     private Set<Registrations> registrations;   
     //getters and setters 
  } 

Events

  @Entity
  public class Events{
     @Id @Column private Integer id;   
     @Column(name="NAME")  private String name;
     @Column(name="DATE")  private Timestamp date;
    @OnetoMany(targetEntity=Registration.class, mappedBy="eId", cascade.....)  
    private Set<Registrations> registrations;   
    //getters and setters 
  }

Registrations

  @Entity
  public class Registrations{
     @Id @Column private Integer id;   
     @Manytoone @JoinColumn(name="pid" ) private Person person;
     @Manytoone @JoinColumn(name="eid" ) private Event event;
  //getters and setters 
  }

So in the input form when the user enters a list of "event id", I need to return a list of people who attended those events.. how to write query for this type? I have this so far..

from Person p join p.registations r where r.event.id in (:eventIdlist)"

but I think it returns both person and refistration objects.. cause when I did this List person = query.list() I got cannot cast kind of error.. I just need person object list..

2
Tell us what the exact error message is, and show use the code which throws this exception. Besides, your mapping is wrong: mappedBy="eId" should be replaced by mappedBy="event", and mappedBy="pId" should be replaced by mappedBy="person".JB Nizet

2 Answers

1
votes
select distinct r.person from Registration r where r.event.id in (:eventIdList)
0
votes

Even simpler:

select r.person from Registration r where r.event.id in (:eventIdList)

EDITED from comment: To join your other tables you could do:

select p
from Person p
where p.registration.event.id in (:eventIdList)
and p.peopleLanguage.language.id in (:languageIdList)