μλ° ORM νμ€ JPA νλ‘κ·Έλλ° : κ΅λ³΄λ¬Έκ³
μλ° ORM νμ€ JPA νλ‘κ·Έλλ° - κΈ°λ³ΈνΈ : μΈνλ°
Java Persistence Query Language
JPA μμ μ 곡νλ SQL μ μΆμνν κ°μ²΄μ§ν₯ 쿼리 μΈμ΄
ν μ΄λΈ λμμ΄ μλ κ°μ²΄ λμμΌλ‘ 쿼리λ₯Ό μμ±νλ€.
SQL κ³Ό μ μ¬νλ€.
- SELECT
- FROM
- WHERE
- GROUP BY
- HAVING
- JOIN
λ±μ μ§μ νλ€.
JPACOMMMIT
μμ μ SQL Query λ‘ λ³νλμ΄ DB μ μ μ©λλ€.
DB
μ μμ‘΄μ μ΄μ§ μλ€.ANSI
νμ€ SQL λλΆλΆμ μ§μνλ€.λ¬Έμμ΄
λ‘ νκΈ° λλ¬Έμ Compile μμ μμ λ¬Έμ λ₯Ό νμ
νκΈ° νλ€λ€.Learning Curve
μ‘΄μ¬ @Entity
public class Member
{
@Id @GeneratedValue
private Long id;
private String username;
private int age;
Entity
,property
λ λμλ¬Έμ ꡬλ³μ νλ€.
Entity
μTable
μ΄λ¦μ κ°μ§ μλ€. ->Entity
μ΄λ¦ μ¨μΌνλ€.
λ³μΉ
μ νμλ€.
TypedQuery
: JPQL κ²°κ³Ό νμ μ΄ μ ν΄μ‘μλ,
TypedQuery
: JPQL κ²°κ³Ό νμ μ΄ μ ν΄μ§μ§ μμ λ,
JPQL κ²°κ³Ό νμ μ΄ μ ν΄μ‘μλ
select m from Member m
select m.username from Member m
---
TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class);
Return
Type μ΄Memeber
νμ (Entity
) νΉμ λ¨μΌν κ° νμ μΈ κ²½μ° μ¬μ©
JPQL κ²°κ³Ό νμ μ΄ μ ν΄μ‘μλ
select m.username, m.age from Member m
---
Query query2 = em.createQuery("select m.username, m.age from Member m");
Return
Type μ΄ λ€μκ³Ό κ°μ΄ λ¬Έμμ΄κ³Ό μ μ λκ°λ‘ λ°μλμ κ°μ΄
μ΄λ νΉμ νμ μ΄λΌκ³ μ νκΈ° μ΄λ €μ΄ μνμμ μ¬μ©νλ€.
쿼리 κ²°κ³Ό κ°μμ λ°λΌμ μ¬μ©νλ λ©μλκ° λ¬λΌμ§λ€
1.getResultList()
- λ°ννλ κ²°κ³Ό κ°μκ° λ³΅μ μΌλ μ¬μ©
- return κ°μ΄
List
- Query κ²°κ³Όκ°μ΄ μ‘΄μ¬νμ§ μμΌλ©΄ ν¬κΈ°κ° 0 μΈ λΉ List λ₯Ό λ°ν
maintry { // Member member = new Member(); // member.setUsername("member1"); // member.setAge(20); // em.persist(member); TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class); List<Member> memberList = query1.getResultList(); System.out.println(memberList.size()); for (Member member1 : memberList) { System.out.println("memberList.size() : "+memberList.size()); }
result
Hibernate: /* select m from Member m */ select member0_.id as id1_0_, member0_.age as age2_0_, member0_.TEAM_ID as TEAM_ID4_0_, member0_.username as username3_0_ from Member member0_ memberList.size() : 0
- μ무κ²λ μ‘°νλμ§ μμ κ²½μ° ν¬κΈ°κ° 0 μΈ
List
λ₯Ό λ°ννλ κ²μ λ³Ό μ μλ€.2. getSingleResult()
- λ¨μΌ κ²°κ³Όκ° λ°ν μ μ¬μ©νλ€.
- κ²°κ³Όκ° λ³΅μμ΄λ©΄ μμΈ λ°μ (
NonUniqueResultException
)
Member 2κ° μμ± ν μ 체 μ‘°νtry { Member member = new Member(); member.setUsername("member1"); member.setAge(20); em.persist(member); Member member2 = new Member(); member2.setUsername("member1"); member2.setAge(20); em.persist(member2); em.flush(); em.clear(); TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class); Member findMember = query1.getSingleResult();
κ²°κ³Ό
javax.persistence.NonUniqueResultException: query did not return a unique result: 2 at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:128) at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1560) at org.example.main.getResultListT.main(getResultListT.java:33)
- κ²°κ³Όκ° μ‘΄μ¬νμ§ μμΌλ©΄ μμΈ λ°μ (
NoResultException
)
Member μμ± νμ§ μκ³ μ‘°νtry { Member member = new Member(); // member.setUsername("member1"); // member.setAge(20); // em.persist(member); // // Member member2 = new Member(); // member2.setUsername("member1"); // member2.setAge(20); // em.persist(member2); em.flush(); em.clear(); TypedQuery<Member> query1 = em.createQuery("select m from Member m", Member.class); Member findMember = query1.getSingleResult();
κ²°κ³Ό
javax.persistence.NoResultException: No entity found for query at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1555) at org.example.main.getResultListT.main(getResultListT.java:33)
κ²°κ³Όκ° μ‘΄μ¬νμ§ μμ μλ μλλ° μμΈκ° λ°μνλ κ²μ λ¬Έμ κ° μλ€.
μμΈμ²λ¦¬λ₯Ό ν΄μ£Όμ΄μΌ νλ€.
Main
try
{
Member member = new Member();
member.setUsername("member1");
member.setAge(20);
em.persist(member);
em.flush();
em.clear();
TypedQuery<Member> query1 = em.createQuery("select m from Member m where m.username = :username", Member.class);
query1.setParameter("username","member1");
List<Member> members = query1.getResultList();
for (Member member1 : members) {
System.out.println("name : "+member1.getUsername());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m
where
m.username = :username */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as TEAM_ID4_0_,
member0_.username as username3_0_
from
Member member0_
where
member0_.username=?
name : member1
Main
try
{
Member member = new Member();
member.setUsername("member1");
member.setAge(20);
em.persist(member);
em.flush();
em.clear();
TypedQuery<Member> query1 = em.createQuery("select m from Member m where m.username = ?1", Member.class);
query1.setParameter(1,"member1");
List<Member> members = query1.getResultList();
for (Member member1 : members) {
System.out.println("name : "+member1.getUsername());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m
where
m.username = :username */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as TEAM_ID4_0_,
member0_.username as username3_0_
from
Member member0_
where
member0_.username=?
name : member1
Parameter λ°μΈλ©μ ν΅ν΄ JPQL μμ λμ μΈ μΏΌλ¦¬λ₯Ό μμ±ν μ μλ€.
μ¬κΈ°μμ΄λ¦
μΌλ‘ λ°μΈλ© νλ λ°©λ²μμΉ
λ‘ λ°μΈλ© νλ λ°©λ² 2κ°μ§λ‘ λλλ€.
μ΄λ¦
μΌλ‘ λ°μΈλ© ν κ²½μ° Parameter μ²λΌ μ¬μ©μ΄ κ°λ₯νλ€.
μμΉ
λ‘ λ°μΈλ© ν κ²½μ° μμ Parameter μ²λΌ μ¬μ©μ΄ κ°λ₯νμ§λ§, μμΉ λ³κ²½ μ μ μ§λ³΄μμ μ΄λ €μμ΄ μκΈ° λλ¬Έμ κ°κΈμ μ΄λ©΄μ΄λ¦
μΌλ‘ λ°μΈλ© νλ κ²μ΄ μ’λ€.
SELECT μ μ μ‘°νν λμμ μ§μ νλ κ²μ λ§νλ€.
@Entity
public class Member
{
@Id @GeneratedValue
private Long id;
private String username;
private int age;
@ManyToOne(fetch = FetchType.LAZY )
@JoinColumn(name = "TEAM_ID")
private Team team;
@Embedded
private Address address;
@Entity
public class Team
{
@Id @GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
@Embeddable
public class Address
{
public Address(){}
public Address(String city, String street, String zipcode)
{
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
private String city;
private String street;
private String zipcode;
select m from Member m
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
List<Member> members = em.createQuery("select m from Member m").getResultList();
for (Member member1 : members) {
System.out.println("name : "+member1.getUsername());
member1.setAge(12);
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m */ select
member0_.id as id1_0_,
member0_.city as city2_0_,
member0_.street as street3_0_,
member0_.zipcode as zipcode4_0_,
member0_.age as age5_0_,
member0_.TEAM_ID as TEAM_ID7_0_,
member0_.username as username6_0_
from
Member member0_
Hibernate:
select
team0_.id as id1_3_0_,
team0_.name as name2_3_0_
from
Team team0_
where
team0_.id=?
name : member1
Hibernate:
/* update
org.example.entity.Member */ update
Member
set
city=?,
street=?,
zipcode=?,
age=?,
TEAM_ID=?,
username=?
where
id=?
Member
μμ λͺ¨λ νλλ€μ μ‘°ν νμΌλ©°, μ‘°ν κ²°κ³Όμ Member λ€μ λͺ¨λ μμμ± Context μμ κ΄λ¦¬ λλ€.
(member1.setAge(12);
νμ λ Update Query λ°μ )
select m.team from Member m
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
List<Team> teams = em.createQuery("select m.team from Member m", Team.class).getResultList();
for (Team team1 : teams) {
System.out.println("name : "+team1.getName());
}
κ²°κ³Ό
Hibernate:
/* select
m.team
from
Member m */ select
team1_.id as id1_3_,
team1_.name as name2_3_
from
Member member0_
inner join
Team team1_
on member0_.TEAM_ID=team1_.id
name : A
Member
μ μ°κ΄κ΄κ³μΈTeam
μ μ‘°ν νλ€. κ°μ²΄ νμ μTeam
μΌλ‘ λμ΄μΌ νλ©°
Query λ°μ μJOIN
μ΄ μλμΌλ‘ λ°μνλ€. μ¬μ©μ μ μ₯μμJOIN
쿼리 κ°λκ°λ κ²μ μΈμ§ μν€κΈ° μν΄μ μ°κ΄κ΄κ³ μ‘°ν μμ λͺ μμ μΌλ‘JOIN
쿼리λ₯Ό μμ±νλ€.
select m.team from Member m Join m.team t
select m.address from Member m
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
List<Address> addresses = em.createQuery("select m.address from Member m", Address.class).getResultList();
for (Address adr : addresses) {
System.out.println("name : "+adr.getCity());
}
κ²°κ³Ό
Hibernate:
/* select
m.address
from
Member m */ select
member0_.city as col_0_0_,
member0_.street as col_0_1_,
member0_.zipcode as col_0_2_
from
Member member0_
name : 1
Member
μEmbedded
νμ μ μ‘°ν
select m.username, m.age from Member m
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member1");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
em.createQuery("select m.username, m.age from Member m").getResultList();
κ²°κ³Ό
Hibernate:
/* select
m.username,
m.age
from
Member m */ select
member0_.username as col_0_0_,
member0_.age as col_1_0_
from
Member member0_
Member
λ€λ₯Έ νμ λ€μ μ‘°ν, μ¦ κ²°κ³Όκ°Query
νμ μ΄λ€.
- Object λ₯Ό λ°ν
List scala = em.createQuery("select m.username, m.age from Member m").getResultList(); Object ob = scala.get(0); Object[] obArr = (Object[])ob; System.out.println("result = "+obArr[0]); System.out.println("result = "+obArr[1]);
κ²°κ³Ό
Hibernate: /* select m.username, m.age from Member m */ select member0_.username as col_0_0_, member0_.age as col_1_0_ from Member member0_ result = member1 result = 20
- DTO λ₯Ό μ€μ ν΄μ κ°μ λ°μ μ μλ€.
DTO μ μpublic class NameAgeDto { private String username; private int age; public NameAgeDto(){} public NameAgeDto(String username, int age) { this.username = username; this.age = age; }
main
List<NameAgeDto> scala = em.createQuery("select new org.example.dto.NameAgeDto(m.username, m.age) from Member m",NameAgeDto.class).getResultList(); NameAgeDto nameAgeDto = scala.get(0); System.out.println("nameAgeDto.getUsername() = "+nameAgeDto.getUsername()); System.out.println("nameAgeDto.getAge() = "+nameAgeDto.getAge());
κ²°κ³Ό
Hibernate: /* select new org.example.dto.NameAgeDto(m.username, m.age) from Member m */ select member0_.username as col_0_0_, member0_.age as col_1_0_ from Member member0_ nameAgeDto.getUsername() = member1 nameAgeDto.getAge() = 20
select new org.example.dto.NameAgeDto(m.username, m.age) from Member m
- μλ‘μ΄ λ°μ΄ν° νμ μ μ μνκ³
- μμμ νμ μ΄ μΌμΉνλ
λ°μ΄ν° νμ μμ±μλ₯Ό μ μ
- λ°μ΄ν° νμ μ κ²½λ‘λ₯Ό ν¬ν¨ν μμ±μλ₯Ό κΈ°μ¬νλ©΄
λ€μκ³Ό κ°μ΄ λ°μ΄ν° νμ μΌλ‘ λ°μ΄ν°λ₯Ό μ»μ μ μλ κ²μ λ³Ό μ μλ€.
setFirstResult
: λͺλ²μ§Έ λΆν° κ°μ Έμ¬ κ²μΈμ§
setMaxResult
: λͺλ²μ§Έ κΉμ§ κ°μ Έμ¬ κ²μΈμ§
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
for(int i = 0; i< 20; i++)
{
Member member = new Member();
member.setUsername("member"+i);
member.setAge(20+i);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
}
em.flush();
em.clear();
List<Member> members = em.createQuery("select m from Member m order by m.age desc", Member.class)
.setFirstResult(0)
.setMaxResults(10)
.getResultList();
System.out.println("members.size() : "+members.size());
for (Member member : members) {
System.out.println("member.getAge() : "+member.getAge());
}
κ²°κ³Ό
Hibernate:
select
team0_.id as id1_3_0_,
team0_.name as name2_3_0_
from
Team team0_
where
team0_.id=?
members.size() : 10
member.getAge() : 39
member.getAge() : 38
member.getAge() : 37
member.getAge() : 36
member.getAge() : 35
member.getAge() : 34
member.getAge() : 33
member.getAge() : 32
member.getAge() : 31
member.getAge() : 30
Member
μ ν¬κΈ°κ°10
μ΄κ³ ,age
κ° ν°Member
λΆν°
λ΄λ¦Ό μ°¨μμΌλ‘ 10 κ°μMember
μ λμ΄λ₯Ό μΆλ ₯ νλ κ²μ λ³Ό μ μλ€.
- JPA λ
DB DIALECT
μ λ°λΌPAGINING
μ²λ¦¬λ₯Ό λ€λ₯΄κ² ν μ μλ€.
select m from Member m inner join m.team t
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m inner join m.team t";
List<Member> members = em.createQuery(query).getResultList();
for (Member member1 : members) {
System.out.println(member1.toString());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m
inner join
m.team t */ select
member0_.id as id1_0_,
member0_.city as city2_0_,
member0_.street as street3_0_,
member0_.zipcode as zipcode4_0_,
member0_.age as age5_0_,
member0_.TEAM_ID as TEAM_ID7_0_,
member0_.username as username6_0_
from
Member member0_
inner join
Team team1_
on member0_.TEAM_ID=team1_.id
Member{id=2, username='member', age=20, address=org.example.entity.Address@45e9b12d}
select m from Member m left outer join m.team t
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m left outer join m.team t";
List<Member> members = em.createQuery(query).getResultList();
for (Member member1 : members) {
System.out.println(member1.toString());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m
left outer join
m.team t */ select
member0_.id as id1_0_,
member0_.city as city2_0_,
member0_.street as street3_0_,
member0_.zipcode as zipcode4_0_,
member0_.age as age5_0_,
member0_.TEAM_ID as TEAM_ID7_0_,
member0_.username as username6_0_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
Member{id=2, username='member', age=20, address=org.example.entity.Address@45e9b12d}
select m from Member m, Team t where m.username = t.name
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m, Team t where m.username = t.name";
List<Member> members = em.createQuery(query).getResultList();
for (Member member1 : members) {
System.out.println(member1.toString());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m,
Team t
where
m.username = t.name */ select
member0_.id as id1_0_,
member0_.city as city2_0_,
member0_.street as street3_0_,
member0_.zipcode as zipcode4_0_,
member0_.age as age5_0_,
member0_.TEAM_ID as TEAM_ID7_0_,
member0_.username as username6_0_
from
Member member0_ cross
join
Team team1_
where
member0_.username=team1_.name
Join μ νν°λ§ ν μ μλ
ON
쑰건 μ μ μ¬μ©ν μ μλ€.
select m from Member m left outer join m.team t on t.name = 'A'
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m left outer join m.team t on t.name = 'A'";
List<Member> members = em.createQuery(query).getResultList();
for (Member member1 : members) {
System.out.println(member1.toString());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m
left outer join
m.team t
on t.name = 'A' */ select
member0_.id as id1_0_,
member0_.city as city2_0_,
member0_.street as street3_0_,
member0_.zipcode as zipcode4_0_,
member0_.age as age5_0_,
member0_.TEAM_ID as TEAM_ID7_0_,
member0_.username as username6_0_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
and (
team1_.name='A'
)
Member{id=2, username='member', age=20, address=org.example.entity.Address@3bc4ef12}
select m from Member m left join Team t on m.username = t.name
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m left join Team t on m.username = t.name";
List<Member> members = em.createQuery(query).getResultList();
for (Member member1 : members) {
System.out.println(member1.toString());
}
κ²°κ³Ό
Hibernate:
/* select
m
from
Member m
left join
Team t
on m.username = t.name */ select
member0_.id as id1_0_,
member0_.city as city2_0_,
member0_.street as street3_0_,
member0_.zipcode as zipcode4_0_,
member0_.age as age5_0_,
member0_.TEAM_ID as TEAM_ID7_0_,
member0_.username as username6_0_
from
Member member0_
left outer join
Team team1_
on (
member0_.username=team1_.name
)
Member{id=2, username='member', age=20, address=org.example.entity.Address@37ad042b}
μΌλ°μ μΈ SQL μμ μ§μνλ
SUB QUERY
λ₯Ό μ§μνλ€.
μ¬λ¬κ°μ§ μ§μν¨μμ κ°μ΄ μ¬μ© ν μ μλ€.
select m.username, m.memberType from Member m where m.memberType = :userType
public enum MemberType {
ADMIN,USER;
}
@Entity
public class Member
{
@Id @GeneratedValue
private Long id;
private String username;
private int age;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TEAM_ID")
private Team team;
@Embedded
private Address address;
@Enumerated(EnumType.STRING)
private MemberType memberType;
try
{
Address address = new Address("1","1","1");
Team team = new Team();
team.setName("A");
em.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(20);
member.setTeam(team);
member.setAddress(address);
member.setMemberType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select m.username, m.memberType from Member m where m.memberType = :userType";
List<Object[]> result = em.createQuery(query)
.setParameter("userType",MemberType.ADMIN)
.getResultList();
System.out.println(result.get(0)[0]);
System.out.println(result.get(0)[1]);
κ²°κ³Ό
Hibernate:
/* select
m.username,
m.memberType
from
Member m
where
m.memberType = :userType */ select
member0_.username as col_0_0_,
member0_.memberType as col_1_0_
from
Member member0_
where
member0_.memberType=?
Parameter Binding κ³Ό ν¨κ» μ¬μ©νμλ€.