开发者

hibernate one to many query

开发者 https://www.devze.com 2023-03-27 01:09 出处:网络
I have following classes-tables with onetomany relationships People @Entity public class People{ @Id @Column private Integer id;

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 refist开发者_高级运维ration objects.. cause when I did this List person = query.list() I got cannot cast kind of error.. I just need person object list..


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


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消