소개
게시글
질문&답변
2023.03.13
flush() 전에 Insert 쿼리 출력되는 현상
package jpabook.jpashop.respository; import jpabook.jpashop.domain.Member; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import java.util.List; @Repository @RequiredArgsConstructor public class MemberRepository { private final EntityManager em; public void save(Member member) { em.persist(member); } public Member findOne(Long id) { return em.find(Member.class, id); } public List findAll() { return em.createQuery("select m from Member m", Member.class) // 엔티티 객체에 대해 조회 .getResultList(); } public List findByName(String name) { return em.createQuery("select m from Member m where m.name = :name", Member.class) .setParameter("name", name) .getResultList(); } }
- 0
- 4
- 1K
질문&답변
2023.03.13
flush() 전에 Insert 쿼리 출력되는 현상
package jpabook.jpashop.service; import jpabook.jpashop.domain.Member; import jpabook.jpashop.respository.MemberRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional(readOnly = true) // jpa 조회 성능 최적화 @RequiredArgsConstructor public class MemberService { private final MemberRepository memberRepository; /** * 회원 가입 */ @Transactional // jpa의 데이터 변경이나 모든 로직들은 가급적이면 transaction 안에서 실행되어야 한다. public Long join(Member member) { validateDuplicateMember(member); memberRepository.save(member); return member.getId(); } private void validateDuplicateMember(Member member) { List findMembers = memberRepository.findByName(member.getName()); if (!findMembers.isEmpty()) { throw new IllegalStateException("이미 존재하는 회원입니다."); } } // 회원 전체 조회 public List findMembers(){ return memberRepository.findAll(); } public Member findOne(Long memberId) { return memberRepository.findOne(memberId); } }
- 0
- 4
- 1K