本文介绍了如何使用HQL或Criteria API从一个到多个对象进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始用例子学习hibernate。
我写了一个团队类,与(玩家的集合)有一对多的关系。



我需要让所有的队伍 p>


  1. 其中玩家名称是X(X不是玩家表的主键)
  2. 玩家Y在哪里

我想我必须通过使用Criteria API或HQL来完成。有人可以告诉我如何实现它。

解决方案

这将适用于 OneToMany 关系,即1个团队可以有多个个玩家,并且每个没有玩家将被映射到多于1个 Team

  @Entity 
public class Team implements Serializable {
私人设置< Player>玩家;
}


@实体
公共类Player实现Serializable {
private String playerName;
}

其中玩家名称是X(X不是播放器表)

 从团队t中选择t内部连接t.players tp WHERE t.id =:id AND tp.playerName = :名称


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.

解决方案

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

这篇关于如何使用HQL或Criteria API从一个到多个对象进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:18