Skip to content

Commit

Permalink
[Week9][Chap3] Feat: 순수 JPA 기반 리포지토리 만들기 (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyjyjy25 committed Nov 21, 2023
1 parent 94009e5 commit 5ac2a25
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

@Repository
public class MemberJpaRepository {
Expand All @@ -17,6 +19,25 @@ public Member save(Member member) {
return member;
}

public void delete(Member member) {
em.remove(member);
}

public List<Member> findAll() {
return em.createQuery("select m from Member m", Member.class)
.getResultList();
}

public Optional<Member> findById(Long id) {
Member member = em.find(Member.class, id);
return Optional.ofNullable(member);
}

public long count() {
return em.createQuery("select count(m) from Member m", Long.class)
.getSingleResult();
}

public Member find(Long id) {
return em.find(Member.class, id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package study.datajpa.repository;

import org.springframework.stereotype.Repository;
import study.datajpa.entity.Member;
import study.datajpa.entity.Team;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

@Repository
public class TeamRepository {

@PersistenceContext
private EntityManager em;

public Team save(Team team) {
em.persist(team);
return team;
}

public void delete(Team team) {
em.remove(team);
}

public List<Team> findAll() {
return em.createQuery("select t from Team t", Team.class)
.getResultList();
}

public Optional<Team> findById(Long id) {
Team team = em.find(Team.class, id);
return Optional.ofNullable(team);
}

public long count() {
return em.createQuery("select count(t) from Team t", Long.class)
.getSingleResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.transaction.annotation.Transactional;
import study.datajpa.entity.Member;

import java.util.List;

import static org.assertj.core.api.Assertions.*;

@SpringBootTest
Expand All @@ -29,4 +31,34 @@ public void testMember() {
assertThat(findMember).isEqualTo(member); // equals hashcode를 오버라이드하지 않았기 대문에 ==비교이다.
}

@Test
public void basicCRUD() {
Member member1 = new Member("member1");
Member member2 = new Member("member2");

memberJpaRepository.save(member1);
memberJpaRepository.save(member2);

// 단건 조회 검증
Member findMember1 = memberJpaRepository.findById(member1.getId()).get();
Member findMember2 = memberJpaRepository.findById(member2.getId()).get();
assertThat(findMember1).isEqualTo(member1);
assertThat(findMember2).isEqualTo(member2);

// 리스트 조회 검증
List<Member> all = memberJpaRepository.findAll();
assertThat(all.size()).isEqualTo(2);

// 카운트 검증
long count = memberJpaRepository.count();
assertThat(count).isEqualTo(2);

// 삭제 검증
memberJpaRepository.delete(member1);
memberJpaRepository.delete(member2);

long deletedCount = memberJpaRepository.count();
assertThat(deletedCount).isEqualTo(0);

}
}

0 comments on commit 5ac2a25

Please sign in to comment.