작성
·
136
0
테스트 코드에서 Member와 Member1을 반영했다고 생각했는데
왜 Member만 DB에 반영되는지 모르겠습니다. ㅠㅠ
@SpringBootTest
class OrderServiceImplTest {
@Autowired
EntityManager em;
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
MemberService memberService = ac.getBean(MemberService.class);
@Test
@Transactional
@Rollback(false)
public void testSaveMember(){
Member member = new Member();
member.setUsername("kin11");
member.setAddress(new Address("1","2","3"));
em.persist(member);
Member member1 = new Member();
member1.setUsername("kin");
member1.setAddress(new Address("1","2","3"));
memberService.join(member1);
}
}
package com.example.spring.repository;
import com.example.spring.domain.Member;
import java.util.List;
public interface MemberRepository {
Long save(Member member);
Member findOne(Long id);
List<Member> findAll();
List<Member> findByName(String username);
}
package com.example.spring.repository;- 먼저 유사한 질문이 있었는지 검색해보세요.
import com.example.spring.domain.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
@Repository
public class MemberRepositoryImpl implements MemberRepository {
@PersistenceContext
EntityManager em;
@Override
public Long save(Member member) {
em.persist(member);
return member.getId();
}
@Override
public Member findOne(Long id) {
return em.find(Member.class, id);
}
@Override
public List<Member> findAll() {
return em.createQuery("select m from Member m",Member.class).getResultList();
}
@Override
public List<Member> findByName(String username) {
return em.createQuery("select m from Member m where m.username =:name",Member.class)
.setParameter("name",username )
.getResultList();
}
}
package com.example.spring.service;
import com.example.spring.domain.Member;
import java.util.List;
public interface MemberService {
Long join(Member member);
List findMembers();
Member findOne(Long Id);
}
package com.example.spring.service;
import com.example.spring.domain.Member;
import com.example.spring.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
//회원 등록
@Override
public Long join(Member member) {
validateDuplicateMember(member);
memberRepository.save(member);
return member.getId();
}
private void validateDuplicateMember(Member member) {
List<Member> findMembers = memberRepository.findByName(member.getUsername());
if (!findMembers.isEmpty()){
throw new IllegalStateException("이미 존재하는 회원입니다.");
}
}
//회원 전체 조회
@Override
@Transactional(readOnly = true)
public List findMembers() {
return memberRepository.findAll();
}
//회원 한명 조회
@Override
@Transactional(readOnly = true)
public Member findOne(Long Id){
return memberRepository.findOne(Id);
}
}
package com.example.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class AppConfig {
}
답변 2
2
MemberService memberService = ac.getBean(MemberService.class);
위의 내용을
@Autowired
MemberService memberService;
로 바꾸시면 원하는 대로 동작하실거에요.
JPA 상에는 문제가 없어보여서 (실제 insert 쿼리도 두번 발생하고) 설마하고 바꿔봤는데
바로 되더라구요.
0
처음 em.persit에서만 insert문이 찍힙니다. ㅠㅠ
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
member
(city, street, zipcode, username, member_id)
values
(?, ?, ?, ?, ?)
https://drive.google.com/file/d/1maSRj9tWHeJYlN2JRPaW0wOZeFgoKWMV/view?usp=sharing
DB를 메모리로 했을 때에는 insert쿼리가 2번 나가지만, H2로 변경했을 때에는 insert문이 왜 1번 나가는지 궁금합니다.
<메모리일때>
<H2일때>
답변감사합니다!!!