1. 엔티티 직접 사용 - 기본키 값
//엔티티 직접 사용
String query1 = "select m from Member m where m = :member";
Member res1 = em.createQuery(query1, Member.class)
.setParameter("member", member1)
.getSingleResult();
System.out.println("member = " + res1);
//엔티티의 아이디를 사용
String query2 = "select m from Member m where m.Id = :memberId";
Member res2 = em.createQuery(query2, Member.class)
.setParameter("memberId", member1.getId())
.getSingleResult();
System.out.println("member = " + res2);
Hibernate:
/* select
m
from
Member m
where
m = :member */ select
member0_.Id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id5_0_,
member0_.type as type3_0_,
member0_.username as username4_0_
from
Member member0_
where
member0_.Id=?
member = Member{Id=3, username='회원1', age=0}
Hibernate:
/* select
m
from
Member m
where
m.Id = :memberId */ select
member0_.Id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id5_0_,
member0_.type as type3_0_,
member0_.username as username4_0_
from
Member member0_
where
member0_.Id=?
member = Member{Id=3, username='회원1', age=0}
2. 엔티티 직접 사용 - 기본키 값
//엔티티 직접 사용
String query3 = "select m from Member m where m.team = :team";
List<Member> res3 = em.createQuery(query3, Member.class)
.setParameter("team", teamA)
.getResultList();
for (Member member : res3) {
System.out.println("team's member = " + member);
}
//엔티티의 아이디를 사용
String query4 = "select m from Member m where m.team.Id = :teamId";
List<Member> res4 = em.createQuery(query4, Member.class)
.setParameter("teamId", teamA.getId())
.getResultList();
for (Member member : res4) {
System.out.println("team's member = " + member);
}
Hibernate:
/* select
m
from
Member m
where
m.team = :team */ select
member0_.Id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id5_0_,
member0_.type as type3_0_,
member0_.username as username4_0_
from
Member member0_
where
member0_.TEAM_ID=?
team's member = Member{Id=3, username='회원1', age=0}
team's member = Member{Id=4, username='회원2', age=0}
Hibernate:
/* select
m
from
Member m
where
m.team.Id = :teamId */ select
member0_.Id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id5_0_,
member0_.type as type3_0_,
member0_.username as username4_0_
from
Member member0_
where
member0_.TEAM_ID=?
team's member = Member{Id=3, username='회원1', age=0}
team's member = Member{Id=4, username='회원2', age=0}