Upload
sunghyun-roh
View
152
Download
2
Embed Size (px)
Citation preview
ORM 은 어떻게 객체를 “매핑”할까
김종인
언제 •2015년 여름
어디서
무엇을 •Modern Data Access for Enterprise Java
공부하게 된 계기
JPA 는 표준 명세
JPA 는 인터페이스의 모음 JPA 2.1 표준 명세를 구현한 3 가지 구현체 하이버네이트 , EclipseLink, DataNucleus
애플리케이션 JPA 표준인터페이스
사용
EclipseLinkHibernate DataNucleus
ORM 이란 ?Object-relational mapping자바의 객체와 RDB 의 테이블을 매핑 시켜주는 개념
JPA : persist (SQL DML INSERT)
JPA 구현체 동작
그외 객체 파서들 ...
GSON : JSON 파서
Jackson : JSON, XML 등 파서
샘플 클래스
Value Object
GSON : JSON 파서
Jackson : JSON, XML 파서
도대체 어떻게 ? 어떤 객체 ? 객체를 들여다 볼수 있는건 없을까 ?
답은 ! Reflection
Reflection (computer programming) 정의
In computer science, reflection is the ability of a com-puter program to examine, introspect, and modify its own structure and behavior at runtime.
런타임시 구조 ( 클래스 ) 와 동작 ( 객체 ) 을 검사하고 수정하는것 .
reflect API
클래스 , 객체들을 통해 투영된 (reflective) 정보들을 가져옴
Java.lang.reflect Provides classes and interfaces for ob-taining reflective information about classes and objects.
샘플 클래스 2
Value Object
클래스 정보 가져오기
슈퍼클래스 정보 가져오기
어노테이션 정보 가져오기
필드 정보 가져오기
Reflection 한번 써보자 !!!
친근한 JDBC ResultSet
친근한 JDBC ResultSet 의 무한 반복
테이블 추가
클래스 추가
ResultSet 작성
친근한 JDBC ResultSet 의 무한 반복 결과
JDBC ResultSet 과 객체 매핑
문제점 private 에 접근 private 은 객체내에서만 사용 가능 정보 은닉 , 캡슐화 위반
해결방안 외부 접근 가능한 통로가 필요함 public Getter/Setter 선언
JDBC ResultSet 과 객체 매핑의 문제점
JavaBean
A JavaBean is just a standard
1. All properties private (use getters/setters)2. A public no-argument constructor3. Implements Serializable.
자바빈 표준에 맞춘 클래스
JavaBean API > BeanDescriptor
JavaBean API > MethodDescriptor
JavaBean API > PropertyDescriptor
JavaBean API > PropertyDescriptor > id
JDBC ResultSet 과 객체 매핑
JDBC ResultSet 과 객체 매핑
리스트로 받아 보자
에러 발생
자바의 한계…
타입 토큰
클래스의 원형을 뜻함 타입 토큰 만으론 제너릭 정보를 가져올수가 없음… 아쉬움… 옆동네 C# 은 된다고함 ...
수퍼 타입 토큰상속한 클래스의 제너릭 정보까지 가져오는 방식
extends 가 된 클래스 ( 슈퍼 클래스 ) 를 가져옴
수퍼 타입 토큰
getSuperClass
Type ParameterizType
제너릭이 있을경우 제너릭이 없음
reflect API
Type 자바의 모든 타입을 받음
ParameterizedType 파라미터가 ( 제너릭 ) 있는 타입이 있을경우
JDBC ResultSet 과 슈퍼타입토큰 매핑
JDBC ResultSet 과 슈퍼타입토큰 매핑
참고자료JPA 자료 : https://www.slideshare.net/zipkyh/ksug2015-jpa1-jpa-51213397Javabean 스택오버플로우 : http://stackoverflow.com/questions/3295496/what-is-a-javabean-exactly리플렉션 ( 위키피디아 ) : https://en.wikipedia.org/wiki/Reflection_(computer_programming)자바 API : http://docs.oracle.com/javase/8/docs/api/타입토큰 :
http://homoefficio.github.io/2016/11/30/%ED%81%B4%EB%9E%98%EC%8A%A4-%EB%A6%AC%ED%84%B0%EB%9F%B4-%ED%83%80%EC%9E%85-%ED%86%A0%ED%81%B0-%EC%88%98%ED%8D%BC-%ED%83%80%EC%9E%85-%ED%86%A0%ED%81%B0/