-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Week9][Chap3] Feat: 순수 JPA 기반 리포지토리 만들기 (#106)
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
김지윤/data-jpa/src/main/java/study/datajpa/repository/TeamRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters