How to retrive objects from one to many using HQL or Criteria API

吃可爱长大的小学妹 提交于 2019-12-25 03:43:41

问题


Started to learn hibernate with examples. I have written a class "Team" that has one to many relationship with (a Collection of) players.

I need to get all the teams

  1. where player name is "X" (X is not the primary key of the Player table)
  2. where player "Y" is in

I think I have to get this done by using Criteria API or the HQL. Can someone tell how I can achieve it.


回答1:


Thsi will work for OneToMany relationship, ie 1 Team can Have many Players, and every no Player will be mapped to more than 1 Team.

@Entity
public class Team implements Serializable {
private Set<Player>    players;
}


@Entity
public class Player implements Serializable {
private String playerName;
}

where player name is "X" (X is not the primary key of the Player table)

SELECT t from Team t inner join t.players tp WHERE t.id = :id AND   tp.playerName = :name


来源:https://stackoverflow.com/questions/26399709/how-to-retrive-objects-from-one-to-many-using-hql-or-criteria-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!