Skip to content

Commit

Permalink
[Week9][Chap3] Feat : 순수 JPA 기반 리포지토리 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dionisos198 committed Nov 20, 2023
1 parent c0f52d4 commit 2a8b3c5
Show file tree
Hide file tree
Showing 3 changed files with 89 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,23 @@ 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 TeamJpaRepository {

@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.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Transactional
Expand All @@ -33,4 +35,30 @@ public void testMember(){

}


@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();
Assertions.assertThat(findMember1).isEqualTo(member1);
Assertions.assertThat(findMember2).isEqualTo(member2);

List<Member> all = memberJpaRepository.findAll();
Assertions.assertThat(all.size()).isEqualTo(2);

long count = memberJpaRepository.count();
Assertions.assertThat(count).isEqualTo(2);

memberJpaRepository.delete(member1);
memberJpaRepository.delete(member2);
long deletedCount = memberJpaRepository.count();
Assertions.assertThat(deletedCount).isEqualTo(0);
}


}

0 comments on commit 2a8b3c5

Please sign in to comment.