我目前正在使用Spring Data JPA和Hibernate开发Spring启动应用程序。我目前的设置如下:
我的TeamMapping存储库:
public interface TeamMembershipMapping Repository extends JpaRepository<TeamMembershipMapping, Integer>
{
public List<TeamMembershipMapping> getByMemberId(Integer memberId, Pageable pageable);
}
My TeamMembershipMapping Entity:
@Entity
@Table(name = "teamMembership")
public class TeamMembershipMapping
{
@Id
private Integer teamMembershipId;
@Column(table = "teamMembership", name = "memberId")
private Integer memberId;
@Enumerated
@Column
private TeamMembershipStatus status;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "teamId")
private Team team;
---Getters and Setters---
}
我的团队实体:
@Entity
@Table(name = "team")
@SecondaryTable(name = "summaryTeam")
public class Team
{
@Id
private Integer teamId;
@Column
private String name;
@Column
private String description;
@Column(table = "summaryTeam")
private Integer wins;
@Column(table = "summaryTeam", nullable = true)
private Integer losses;
--Getters and Setters---
}
现在我的问题是,当我调用getByMemberId来检索特定成员拥有的所有TeamMembershipMappings时,Hibernate会生成一个sql选择以获取所有TeamMappings,然后为每个Mappings执行sql选择以获取Team信息。因此,如果该成员是4个团队的一部分,那么1 + 4 sql选择何时可以在一个团队中完成。
那么如何强制Hibernate在一个大连接选择中获取所有信息?
答案 0 :(得分:2)
在实体TeamMembershipMapping
中,您可以创建一个这样的命名查询:
select tmm.team from TeamMembershipMapping tmm where tmm.memberId = :memberId;
这只是一个简单的查询。