JPA ์ํฐํฐ์์ ์์ฑ์๋ฅผ ์ฌ๋ฌ ๊ฐ ๋ง๋ค์ด ๋์ผ๋ฉด ๊ธฐ๋ณธ์์ฑ์(@NoArgsConstructor
๋ฅผ ์ ์ํด์ผํ๋ค๋ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
+ public, protected๋ก ์ ์ธํด์ผํจ !
๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ ์ํด์ผํ๋ ์ด์ ๋ฅผ ์ ํํ๊ฒ ์ค๋ช ํ ์ ์์ด์ ์ ๋ฆฌํ๋ ์๊ฐ์ ๊ฐ์ ธ๋ณด๊ฒ ๋ค.
๐ ๊ฒฐ๋ก ๋ถํฐ ๋งํ๋ฉด Java Reflection API์์ ๊ธฐ๋ณธ์์ฑ์๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ค.
JPA๋ DB ๊ฐ์ ๊ฐ์ฒด ํ๋์ ์ฃผ์ ํ ๋, ๋์ ์ผ๋ก ์ํฐํฐ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ ๊ธฐ๋ณธ์์ฑ์๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ Reflection์ ์ฌ์ฉํ๋ค๊ณ ํ๋ค.
์๋ฐ์์๋ Reflection์ด๋ผ๋ API๋ฅผ ์ ๊ณตํ๋๋ฐ, ๊ทธ๋ผ Reflection์ ๋ญ๊น....
Java Reflection
์๋ฐ์์๋ Reflection์ด๋ผ๋ API๋ฅผ ์ ๊ณตํ๋ค.
Reflection์ ์ ์์ ๋ํด ์ค๋ช ํ ๊ฒ์ ๋ณด๋ฉด ๐ Reflection
Java reflection allows us to inspect and/or modify runtime attributes of classes, interfaces, fields and methods.
This particularly comes in handy when we don’t know their names at compile time.
Additionally, we can instantiate new objects, invoke methods and get or set field values using reflection.
์ด๋ ๊ฒ ๋์์๋ค.
์ฆ, ์ปดํ์ผ ์์ ์ ๊ตฌ์ฒด์ ์ธ ํด๋์ค ํ์ ์ ์์ง ๋ชปํด๋ ๊ทธ ํด๋์ค์ ์ ๋ณด(์์ฑ, ๋ฉ์๋, ์ธํฐํ์ด์ค, ํ๋ ๋ฑ)์ ์ ๊ทผํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ์ด๋ค.
"๊ตฌ์ฒด์ ์ธ ํด๋์ค ํ์ ์ ์์ง ๋ชปํด๋ ํด๋์ค ์ ๋ณด์ ์ ๊ทผ ๊ฐ๋ฅํ๋ค"๋ผ๋ ๋ง์ ์๋ฏธ๊ฐ ์ฒ์์ ๋ฐ๋ก ์ดํด๋์ง ์์๋ค.
์์๋ฅผ ํตํด ์์๋ณด์.
public class Person{
private final String name;
private int weight;
public Person(String name, int weight){
this.name = name;
this.weight = weight;
}
public void gain(){
this.weight++;
}
public int getWeight(){
return weight;
}
}
public static void main(String[] args){
Object obj = new Person("Bob", 59);
}
์๋ฐ์ "๋คํ์ฑ" ํน์ง์ผ๋ก Person ๊ฐ์ฒด๋ Object ํด๋์ค์ ์ธ์คํด์ค์ ํ ๋น๋ ์ ์์ง๋ง Person ํด๋์ค์ ์ ์๋ ๋ฉ์๋(gain)๋ ํธ์ถํ ์ ์๋ค.
์๋ฐ๋ ์ ์ ์ธ์ด๋ก์ ์ปดํ์ผ ํ์์ ๊ฐ์ฒด ํ์ ์ด ๊ฒฐ์ ๋๋ฏ๋ก ์ปดํ์ผ๋ฌ๋ ํด๋น ๋ณ์์ ํ์ ์ ๊ธฐ๋ฐ์ผ๋ก ๋ฉ์๋ ํธ์ถ์ ๊ฒฐ์ ํ๋ค. obj์ ๋ํด Object ํด๋์ค์ ์ ์๋ ๋ฉ์๋๋ง ์ฌ์ฉํ ์ ์๋ค๊ณ ์ถ๋ก ํด Person ํด๋์ค๋ผ๋ ๊ตฌ์ฒด์ ์ธ ํ์ ์ ๋ชจ๋ฅด๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๊ตฌ์ฒด์ ์ธ ํ์
์ ๋ชจ๋ฅด๋ ํด๋น ํด๋์ค์ ์ ๋ณด์ ์ ๊ทผํ ์ ์๊ฒ ๋๋ ๊ฒ์ด๋ค!
์ด๊ฒ ์๋ ์ ์์ ์ธ๊ฑด๋ฐ, Reflection API๋ ๊ตฌ์ฒด์ ์ธ ํ์
์ ๋ชฐ๋ผ๋ ํด๋น ํด๋์ค์ ์ ๋ณด์ ์ ๊ทผํ ์ ์๊ฒ ๋ง๋ค์ด์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค.
๋ํ์ ์ธ ์ ์ฉ ์ฌ๋ก๋
- JPA์์ DB ๊ฐ์ ๊ฐ์ฒด ํ๋์ ์ฃผ์ ํ ๋
- xxRepository.findById์ ๊ฐ์ ๋ฉ์๋๋ฅผ ํตํด ๊ฐ์ฒด ์กฐํํ ๋
- @RequestBody ์ฌ์ฉ ์ DTO ๊ฐ์ฒด์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ๋
- ํ ์คํธ ์ผ์ด์ค ์์ฑ ์ private ๋ฉ์๋๋ฅผ ํ ์คํธํ ๋
๋ฑ์ด๋ค.
Reflection์ ๋ํ ์์ธํ ๊ฐ๋ ์ ๋ฆฌ๋ ๋ค์์ ๊ธฐ๋กํ๋๋ก ํ๊ฒ ๋ค.
๊ธฐ๋ณธ์์ฑ์๋ฅผ ์ ์ํด์ผํ๋ ์ด์
๐ Reflection์ ์์ฑ์์ ๋งค๊ฐ๋ณ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์๋ฐ์์ JVM์ด ์คํ๋๋ฉด ์์ฑ๋ ์๋ฐ ์ฝ๋๊ฐ static ์์ญ์ ์ ์ฅ๋๋ค.
Reflection API๋ ์ด ์ ๋ณด๋ฅผ ํ์ฉํ์ฌ ๊ตฌ์ฒด์ ์ธ ํด๋์ค ํ์ ์ ์์ง ๋ชปํด๋ ํด๋์ค ์ด๋ฆ์ ํตํด static ์์ญ์์ ๊ทธ ํด๋์ค์ ์ ๋ณด(๋ฉ์๋, ํ์ ๋ณ์ ๋ฑ)์ ์ ๊ทผํ ์ ์๊ฒ ํ๋ค.
ํ์ง๋ง Reflection์ด ๊ฐ์ ธ์ฌ ์ ์๋ ์ ๋ณด๊ฐ ์๋ค. ๋ฐ๋ก ์์ฑ์์ ๋งค๊ฐ๋ณ์ ์ ๋ณด์ด๋ค.
๊ทธ๋ฌ๋ฏ๋ก Reflection์ผ๋ก ์์ฑํ ๊ฐ์ฒด์ ๋ชจ๋ ํ๋๋ฅผ ๋ฐ๋ ์์ฑ์๊ฐ ์๋๋ผ๋ ํด๋น ์์ฑ์๋ฅผ ํธ์ถํ ์ ์๊ณ ๊ฐ์ฒด ์์ฑ ์์ฒด๋ฅผ ํ ์ ์๋ค!
๊ธฐ๋ณธ์์ฑ์๊ฐ ์ ์๋์ด์ผ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉฐ ๊ทธ ํ ํ๋๊ฐ ๋ฑ์ Reflection์ผ๋ก ๋ฃ์ด์ค ์ ์๋ ๊ฒ์ด๋ค.
๊ธฐ๋ณธ์์ฑ์๊ฐ public, protected์ด์ด์ผ ํ๋ ์ด์
๐ Proxy์ Lazy Loading ๋๋ฌธ์ด๋ค.
์ง์ฐ๋ก๋ฉ๊ณผ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์์ฑ์๊ฐ private์ด์ด์๋ ์๋๋ค.
์ฆ์ ๋ก๋ฉ(EAGER)์ผ ๊ฒฝ์ฐ์๋ ์ค์ ์ํฐํฐ ๊ฐ์ฒด๋ฅผ ์กฐํํ๊ธฐ ๋๋ฌธ์ ์๊ด ์์ง๋ง, ์ง์ฐ ๋ก๋ฉ(LAZY)์ธ ๊ฒฝ์ฐ์๋ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผํ๋ค.
ํ๋ก์๋ ์๋ณธ ์ํฐํฐ๋ฅผ ์์ํด์ ํ๋ก์ ์ํฐํฐ๊ฐ ์๋ณธ ์ํฐํฐ๋ฅผ ์ฐธ์กฐํ๋๋ก ํ์ฌ ์ฌ์ฉํ๋ค.
์์ํ ๊ฐ์ฒด์ ์์ฑ์๋ ๋ฐ๋์ ๋ถ๋ชจ ๊ฐ์ฒด์ ์์ฑ์ super๋ฅผ ํธ์ถํด์ผํ๋๋ฐ private์ด๋ฉด ์์๋ฐ์ ํด๋์ค์์ ํธ์ถํ ์ ์๋ค.
๊ทธ๋ฌ๋ฏ๋ก ๊ธฐ๋ณธ์์ฑ์๊ฐ private์ผ๋ก ์ ์ธ๋์ด ์๋ค๋ฉด ํด๋น ์ํฐํฐ๋ฅผ ์์ํ ํ๋ก์๋ฅผ ๋ง๋ค ์ ์๋ ๊ฒ์ด๋ค.
๊ทธ๋์ Entity ํด๋์ค์ ์์ฑ์๋ public ๋๋ protected์ผ๋ก ์ ์ธํด์ผํ๋ค !
์ฐธ๊ณ ์๋ฃ
https://www.baeldung.com/java-reflection
https://tecoble.techcourse.co.kr/post/2020-07-16-reflection-api/
https://www.baeldung.com/jpa-no-argument-constructor-entity-class
'Database > JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] @Column(unique=true)์ @UniqueConstraint (0) | 2024.05.01 |
---|---|
[JPA] Open-in-view์ @Transactional (0) | 2024.04.08 |