Skip to content

Commit

Permalink
[Week9][Chap3] Feat : 순수 JPA 기반 리포지토리 생성 - Member, Team (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
songhee1 committed Nov 20, 2023
1 parent b0acc19 commit 5846410
Show file tree
Hide file tree
Showing 3 changed files with 98 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 @@ -16,6 +18,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,42 @@
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,10 +8,13 @@
import org.springframework.transaction.annotation.Transactional;
import study.datajpa.entity.Member;

import java.util.List;

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

@SpringBootTest
@Transactional
@Rollback(false)
class MemberJpaRepositoryTest {
@Autowired MemberJpaRepository memberJpaRepository;

Expand All @@ -26,4 +29,36 @@ public void testMember(){
assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
assertThat(findMember).isEqualTo(member);
}

@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);

member1.setUsername("name changed!!");
// //리스트 조회 검증
// 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 count2 = memberJpaRepository.count();
// assertThat(count2).isEqualTo(0);
}
}

0 comments on commit 5846410

Please sign in to comment.