해결된 질문
작성
·
329
0
@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<Member> findAll() {
return em.createQuery("select m from Member m", Member.class).getResultList();
}
public List<Member> findByName(String name) {
return em.createQuery("select m from Member m where m.name = :name", Member.class)
.setParameter("name", name)
.getResultList();
}
}
@Service
@RequiredArgsConstructor
@Transactional
public class MemberService {
private final MemberRepository memberRepository;
public Long join(Member member) {
validateDuplicateMember(member); //회원이름 중복인지 검증
memberRepository.save(member);
return member.getId();
}
//회원이름 중복검사
public void validateDuplicateMember(Member member) {
List<Member> findMember = memberRepository.findByName(member.getUsername());
if (findMember.size() > 0) {
throw new IllegalStateException("중복된 이름이 있습니다.");
}
}
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
public class MemberServiceTest {
@Autowired
MemberService memberService;
@Autowired
MemberRepository memberRepository;
@Autowired
EntityManager em;
@Test
@Rollback(false)
public void join() {
Member memberA = new Member();
memberA.setUsername("memberA");
memberRepository.save(memberA);
List<Member> findMember = memberRepository.findAll();//통과
memberRepository.findByName(memberA.getUsername());
}
}
답변 1
1
앗..찾았습니다..
저는 Member에서
private String username이 되어있어서
select m from jpabook.jpashop.domain.Member m where m.name = :name
에서
where m.name이.. 아니라 where m.username이었네요.. 하하;;
relate16님 스스로 잘 해결하셨습니다^^!