source 는 Github 에 있습니다.
@Slf4j
@RequiredArgsConstructor
@Service
public class MemberService {
private final MemberRepository memberRepository;
@Transactional(readOnly = true)
public List<Member> getMembers2() {
List<Member> members = memberRepository.findAll();
members.forEach(member -> {
log.info("member : {}, account.size : {}", member, member.getAccounts().size());
});
return members;
}
}
@Entity
@Getter
@NoArgsConstructor
public class Member {
@Id
@GeneratedValue
@Column(name = "member_id")
@Setter
private Long id;
@Column
private String name;
@Column
private String telNo;
@Column
private int age;
@JsonIgnore
@OneToMany(mappedBy = "member")
private List<Account> accounts = new ArrayList<>();
}
@Entity
@Getter
@NoArgsConstructor
public class Account {
@Id
@GeneratedValue
@Column(name = "account_id")
private Long id;
@Column @Setter
private String accountNo; // 계좌번호
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
}
@Entity
public class Member {
@OneToMany(mappedBy = "member")
@BatchSize(size = 100)
private List<Account> accounts = new ArrayList<>();
}
spring:
jpa:
properties:
hibernate:
default_batch_fetch_size: 100
select
accounts0_.member_id as member_i3_0_1_,
accounts0_.account_id as account_1_0_1_,
accounts0_.account_id as account_1_0_0_,
accounts0_.account_no as account_2_0_0_,
accounts0_.member_id as member_i3_0_0_
from
account accounts0_
where
accounts0_.member_id in (
?, ?
)