When to Use a Regular Join?

329 단어·2 분·원문(.md)

When looking at solutions for the N+1 problem or the advantages of fetch joins, fetch joins generally seem better than regular joins.

However, regular joins definitely have their uses. Since JPA fundamentally requires careful consideration of DB <-> object consistency, only entities strictly necessary for the logic should be kept in the persistence context.

Therefore, rather than indiscriminately using fetch joins to load everything into the persistence context, it's a good way to prevent unnecessary malfunctions by appropriately using regular joins to load only the necessary entities into the persistence context.

In cases like the example below, a regular Join is much more effective than a Fetch Join.

Example #

Retrieve the Team to which a member named 'team2member4' belongs (member's information is not needed)

This is a situation where an associated entity is needed for the query search condition but its actual data is not required.

As explained previously, regular joins do not load the joined target into the persistence context.

This characteristic of regular joins seems suitable for the situation that needs to be resolved in this example.

Let's execute the code below.

(The JPQL used in the example below is for illustrative purposes, and JPQL that applies conditions to collections like this should be avoided.)

// TeamRespotory.java
@Query("SELECT distinct t FROM Team t join t.members m where m.name = :memberName")
public List<Team> findByMemberNameWithMemberUsingJoin(String memberName);

// TeamService.java
@Transactional
public List<Team> findByMemberNameWithMemberUsingJoin(String memberName){
  return teamRepository.findByMemberNameWithMemberUsingJoin(memberName);
}

// FetchJoinApplicationTests.java
@Test
public void joinConditionTest() {
  List<Team> memberUsingJoin = teamService.findByMemberNameWithMemberUsingJoin("team2member4");
  System.out.println(memberUsingJoin);
}

Actually, the above example throws a LazyInitializationException for the same reason as the previous regular join example, but if we set a breakpoint and check the result,

A Team named 'team2' containing a member named 'team2member4' was retrieved.

As expected, the Member used in the regular join shows an uninitialized state.

Thus, it can be seen that actively using regular joins is efficient when an associated Entity is only used in search conditions.

Back-End/jpa/일반Join은언제.md