我想询问 N+1 问题的解决方案。我有具有 1:M 关系的 Account 表和 Account_role 表。我通过在@Query 中使用 LEFT JOIN FETCH 尝试连接获取方法,但不起作用。
账户类:
@Entity(name = "account")
@Table(name = "account")
public class AccountBean implements Serializable {
@Id
@Column("username")
private String username
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false)
private Set<AccountRoleBean> roles = new HashSet<>();
// getters setters
}
帐户角色类:
@Entity(name = "account_role")
@Table(name = "account_role")
public class AccountRoleBean implements Serializable {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String username;
// getters setters
}
帐户存储库类
public interface AccountRepo extends JpaRepository<AccountBean, String> {
@Query("FROM account a LEFT JOIN FETCH account_role role ON a.username = role.username WHERE a.username = :username")
AccountBean findAccountWithRoles(String username);
}
输出
Hibernate:
select
accountbea0_.username as username1_0_0_,
accountrol1_.id as id1_1_1_,
accountbea0_.is_active as is_activ2_0_0_,
accountbea0_.last_login_date as last_log3_0_0_,
accountbea0_.pw as pw4_0_0_,
accountrol1_.username as username3_1_1_,
accountrol1_.role_name as role_nam2_1_1_
from
account accountbea0_
left outer join
account_role accountrol1_
on (
accountbea0_.username=accountrol1_.username
)
where
accountbea0_.username=?
Hibernate:
select
roles0_.username as username3_1_0_,
roles0_.id as id1_1_0_,
roles0_.id as id1_1_1_,
roles0_.username as username3_1_1_,
roles0_.role_name as role_nam2_1_1_
from
account_role roles0_
where
roles0_.username=?