F.Y.R

[Error] org.hibernate.LazyInitializationException: could not initialize proxy

๐Ÿ˜€  ์›์ธ ํŒŒ์•…

์ฝ”๋“œ๋ฅผ ์—ด์‹ฌํžˆ ์ž‘์„ฑํ•ด์„œ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›๊ณ , ์ฆ๊ฑฐ์šด ๋งˆ์Œ์œผ๋กœ ๋ผ์ด๋ธŒ ๋ฐฐํฌ๋ฅผ ํ–ˆ๋‹ค.. 

๊ทธ๋Ÿฌ๋‚˜ ๋ช‡ ๋ถ„์ด ์ง€๋‚˜์ง€ ์•Š๊ณ  error alert์ด ๋‚˜์˜ค๊ธฐ ์‹œ์ž‘ํ–ˆ๊ณ ,,, ์ •๋ง ์–ด์ฒ˜๊ตฌ๋‹ˆ ์—†๋Š” ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค.

 

์—๋Ÿฌ ๋‚ด์šฉ์€ JPA์—์„œ ๋‚˜์˜จ ์˜ค๋ฅ˜์˜€๋‹ค.. 

org.hibernate.LazyInitializationException: could not initialize proxy 

 

์œ„์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด ์›์ธ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ตฌํ˜„ํ•œ ๋ถ€๋ถ„์„ ๋‹ค์‹œ ๋ณด์•˜๊ณ ,

Entity ๊ฐ์ฒด๋ฅผ DTO ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด BeanUtils copyProperties๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ๋ถ€๋ถ„ํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.. (๊ดœํžˆ ๋น ๋ฅด๊ฒŒ ํ•˜๋ ค๋‹ค๊ฐ€..)

 

BeanUtils copyProperties๋ž€?

์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” BeaUtils ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์ธ๋ฐ, ๊ฐ™์€ ์ด๋ฆ„์€ getter์™€ setter๋งŒ ์ž˜ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค๋ฉด ์†์‰ฝ๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

( ์ฐธ๊ณ  : https://zzang9ha.tistory.com/304 )

 

์ด ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ฐ„๊ณผํ•œ ๋ถ€๋ถ„์ด ํ•œ ๊ฐ์ฒด๋ฅผ ๋‹ค๋Œ€์ผ ๊ด€๊ณ„๋กœ ๊ตฌ์„ฑํ–ˆ๊ณ  ์ด๋ฅผ LAZY ํŽ˜์น˜๋ฅผ ํ•˜๋„๋ก ์„ค์ •ํ•ด๋‘์—ˆ์—ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ์ฒด๋Š” ํ”„๋ก์‹œ ๊ฐ์ฒด๋กœ ์ €์žฅ์ด ๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์˜€๊ณ , ๊ทธ๋ž˜์„œ ์œ„ ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰์‹œ ์›๋ณธ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๋ ค๊ณ  ์‹œ๋„ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด ์—ˆ๋‹ค......!

 

 

๐Ÿ˜€  ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๋ญ... Lazy ๋ฅผ Eager๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ข‹๊ฒ ์ง€๋งŒ.. ๊ทธ๋ ‡๊ฒŒ ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค..

๊ทธ๋ž˜์„œ set ํ•จ์ˆ˜๋กœ ํ•˜๋‚˜ ํ•˜๋‚˜ ์•„๋ž˜์™€ ๊ฐ™์ด ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ( ๊ฐ€์žฅ ์›์ดˆ์ ์ธ ํ•ด๊ฒฐ์ฑ… )

 

๋˜ํ•œ copyProperties ์ค‘ ํ•˜๋‚˜์˜ overloadingํ•œ ๋ฉ”์†Œ๋“œ๋กœ ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด ๋ณต์‚ฌ์—์„œ ์ œ์™ธ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ,

 ์œ„์— ๋‚˜์˜จ ๊ฒƒ๊ณผ ๊ฐ™์ด 3๋ฒˆ์งธ ์ธ์ž๋กœ ์ œ์™ธํ•˜๊ณ  ์‹ถ์€ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ๋ฉด ์ œ์™ธ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด๊ฒฐํ•ด๋ณด์•˜๋‹ค.

@Entity
public class Team {
	@Id
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    private Member member;
}

public class Member {
	@Id
    private Long id;
}

//As-is 
public TeamDto toDto(Team team) {
	TeamDto teamDto = new TeamDto();
    BeanUtil.copyProperties(team, teamDto);
    return teamDto;  
}

//to-be
public TeamDto toDto(Team team) {
	TeamDto teamDto = new TeamDto();
    BeanUtil.copyProperties(team, teamDto, "member");
    // MemberDto ๋งŒ๋“ค๊ธฐ
    MemberDto memberDto = new MemberDto();
    memberDto.setId(team.getMember().getid());
    teamDto.setMemberDto(memberDto)
}

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Lazy๋กœ ๊ฐ€์ ธ์˜จ ํ”„๋ก์‹œ ๊ฐ์ฒด ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์–ด ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋‚˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค

 

( ํ˜น์‹œ ๋” ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์ด ์žˆ๋‹ค๋ฉด,,, ๋ฐ”๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค )

๋ฐ˜์‘ํ˜•

'F.Y.R' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[F.Y.R] Jitter? ( Retry์˜ ์ „๋žต )  (0) 2022.01.16
[Error] Cause: java.lang.ExceptionInInitializerError  (0) 2021.10.30