1

I am currently working on a spring boot app using Spring Data JPA and Hibernate. My current setup looks like:

My TeamMapping Repository:

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---
}

And my Team Entity:

@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---
}

Now my issue is that when I call getByMemberId to retrieve all the TeamMembershipMappings that a particular member has, Hibernate is generating an sql select to get all the TeamMappings, then doing an sql select for each of those Mappings to get the Team information. So if the member is part of 4 teams, thats 1+4 sql selects when it could be done all in one join.

So how do I force Hibernate to scoop up all the information in one big join select?

OpIvy
  • 37
  • 1
  • 9
  • From your entity of TeamMembershipMapping, I see that every member can only have one team, because the team is set to `@OneToOne(fetch = FetchType.EAGER)`. – Qianyue Mar 12 '15 at 21:01
  • Each member can have multiple teams. TeamMembershipMapping is just the connecting table of a Many-to-Many relationship between Member and Team. In this current state I am getting multiple team memberships returned when selecting by memberId, its the SQL query optimization that I am concerned with. – OpIvy Mar 12 '15 at 21:25
  • What does the implementation of getByMemberId look like? HQL? – raminr Mar 12 '15 at 21:39
  • This is called "n+1", searching for "hibernate n+1 join" or something similar should give you good results. Here is one: http://stackoverflow.com/a/19061378/923847 – Matsemann Mar 12 '15 at 21:48

1 Answers1

2

In the entity TeamMembershipMapping, you can create a namedQuery like this :

select tmm.team from TeamMembershipMapping tmm where tmm.memberId = :memberId;

It is just a simple query.

Qianyue
  • 1,767
  • 19
  • 24
  • Is there any other way to force a join besides a namedQuery? Perhaps an annotation on the member variables? I'd like to avoid a namedQuery, it seems messy to me. – LostAtSea Dec 10 '19 at 17:19