hibernate get join column value → 필드 접근 방식 vs property 접근 방식
- 의문점)
- 프로젝트 진행 도중 many to one 관계(a:b = 1:n) 로 묶인 a와 b클래스에 대해 b클래스에서 조인을 하지 않고 a 클래스의 id값만 가져오게 할 수 있는지 궁금했다.
- 찾아본 결과, 우리가 평소 별다른 의식을 하지 않고 사용하던
b.getA().getId()이 친구가 키가 된단 걸 확인했다.
- 해결)
- 프록시 객체도 자체적으로 id값을 가지고 있다.
- file access와 property access 방식 중 property 방식을 사용하면
b.getA().getId()라고 했을 때 프록시 객체의 id 값을 바로 가져올 수 있다.
- 참고)
- proxies contain the id anyway. To get the id of an
Aproxy without initializing it, first declare the id to be accessed via property: - Changing access type for the id is necessary because if you use field access, Hibernate does not distinguish
getId()method from other ordinary methods (which trigger proxy initialization when invoked)
인용
