인프런 커뮤니티 질문&답변

즐거운 거위님의 프로필 이미지
즐거운 거위

작성한 질문수

스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술

MemberServiceIntegrationTest 실행 오류 (join함수)

해결된 질문

작성

·

291

0

스프링 DB접근기술 섹션에 JPA 파트를 듣고 있는데

MemberServiceIntegrationTest.java 파일에서 join함수를 실행했는데 아래와 같은 오류가 발생하여 질문올립니다.

Spring Boot : 2.7.9

Java : 11

캡처.JPG

application.properties

spring.datasource.url= jdbc:h2:tcp://localhost/D:\\workspace\\study\\test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

 

spring.hellomyspring.domain.Member

package spring.hellomyspring.domain;

import javax.persistence.*;

@Entity
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

 

spring.hellomyspring.controller.Member

package spring.hellomyspring.controller;

public class Member {

    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

spring.hellomyspring.repository.JpaMemberRepository

package spring.hellomyspring.repository;

import spring.hellomyspring.controller.Member;

import javax.persistence.EntityManager;
import java.util.List;
import java.util.Optional;

public class JpaMemberRepository implements MemberRepository{

    private final EntityManager em;

    public JpaMemberRepository(EntityManager em) {
        this.em = em;
    }


    @Override
    public Member save(Member member) {
        em.persist(member);
        return member;
    }

    @Override
    public Optional<Member> findById(Long id) {
        Member member = em.find(Member.class, id);
        return Optional.ofNullable(member);
    }

    @Override
    public Optional<Member> findByName(String name) {
        List<Member> result = em.createQuery("select m from Member m where m.name = :name", Member.class)
                .setParameter("name", name)
                .getResultList();
        return result.stream().findAny();
    }

    @Override
    public List<Member> findAll() {
        return em.createQuery("select m from Member m", Member.class).getResultList();
    }
}

 

spring.hellomyspring.service.MemberService

package spring.hellomyspring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spring.hellomyspring.controller.Member;
import spring.hellomyspring.repository.MemberRepository;
import spring.hellomyspring.repository.MemoryMemberRepository;

import java.util.List;
import java.util.Optional;

@Transactional
public class MemberService {

    private final MemberRepository memberRepository;


    public MemberService(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    public Long join(Member member){
        // 같은 이름 중복 회원 x
        validateDuplicateMember(member);

        memberRepository.save(member);
        return member.getId();
    }

    private void validateDuplicateMember(Member member) {
        memberRepository.findByName(member.getName())
                .ifPresent(m -> {
                    throw new IllegalStateException("이미 존재하는 회원입니다.");
                });
    }


    // 전체회원 조회
    public List<Member> findMembers() {
        return memberRepository.findAll();
    }


    public Optional<Member> findOne(Long memberId) {
        return memberRepository.findById(memberId);
    }
}

답변 1

0

안녕하세요. 즐거운 거위님, 공식 서포터즈 OMG입니다.
.

image

image

해당 클래스 첫번째 import인 spring.hellomyspring.controller.Member가 아닌, spring.hellomyspring.domain.Member로 변경해주세요
.
감사합니다.

 

즐거운 거위님의 프로필 이미지
즐거운 거위

작성한 질문수

질문하기