ORM
ORM은 Object Relational Mapping의 줄임말로 "객체 관계 매핑"이라는 뜻이며 객체와 RDB의 데이터를 매핑해주는 역할을 한다. persistent API라고도 한다.(JPA, Hibernate 등)
프로그래밍은 객체지향으로 하는데 그 데이터는 RDB에 맞춘 데이터 기반 저장을 해야 하므로 문제점이 발생한다.
(DB Data <--Mapping--> Object)
이 것을 패러다임 불일치라고 하며 개발자가 직접 이 문제를 해결하기엔 비용이 많이 든다.
그래서 ORM이 탄생했다.
ORM의 장점
1. 비즈니스 로직에 더 집중할 수 있다.
2. 재사용 및 유지보수의 편리성이 높다.
3. 한 가지 DB에 의존하지 않아도 된다.
ORM의 단점
1. 잘못 사용할 경우 오히려 성능이 안 좋아진다.
2. DB도 알아야 하고 ORM도 알아야 한다.
JPA
JPA는 Java Persistence API의 줄임말로 자바에서 사용하는 ORM 표준이라는 뜻이다.
인터페이스의 모음이므로 구현체가 따로 있어야 하는데 현재는 Hibernate가 가장 큰 비중을 차지하고 있는 구현체이다.
JPA는 애플리케이션과 JDBC 사이에서 동작한다. JPA를 사용하면 JDBC API를 통해 SQL을 호출해 DB와 통신한다.
과거에는 EJB가 자바 표준 ORM이었지만 의존성 문제, 가독성, 복잡한 사용방법, 속도 등 문제가 많은 ORM이었다.
JPA는 이러한 문제들을 해결하고 객체지향다운 코딩을 할 수 있게 만들어준다.
JPA는 ORM의 한 종류이기 때문에 장점과 단점이 동일하지만 과거 EJB의 경우에는 단점만 부곽 되었기에 JPA가 많이 사용되고 있다.
Spring Data JPA
Spring Data JPA는 JPA를 한 단계 더 추상화시켜 개발 용이성을 상당히 올려주는 인터페이스다.
Spring Data JPA는 'Repository'라는 인터페이스를 제공한다.
이 Repository는 정해진 규칙으로 인터페이스를 선언하기만 해도 자동으로 내부에서 구현체를 만들어 동작시켜준다.
특히 자주 사용하는 매서드들은 별도로 선언하지 않아도 사용할 수 있다.
사용법은 다음과 같다.
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname,findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age ⇐ ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> age) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
출처 : https://gmlwjd9405.github.io/2019/02/01/orm.html , https://gmlwjd9405.github.io/2019/08/04/what-is-jpa.html, https://docs.spring.io/spring-data/jpa/docs/1.10.1.RELEASE/reference/html/#jpa.sample-app.finders.strategies
'CS' 카테고리의 다른 글
GSLB와 CDN (0) | 2024.01.17 |
---|---|
쓰레드 특징 (프로세스와 비교) (1) | 2024.01.12 |
DB Index (0) | 2022.05.27 |
REST API (0) | 2022.05.22 |
해시 맵(개별 체이닝, 오픈 어드레스) (0) | 2022.05.18 |