• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

스프링 데이터 JPA 와 EntityManager

21.12.14 10:34 작성 조회수 233

0

안녕하세요 영한님 강의 잘 들었습니다! 

 

제가 Data JPA랑 entity Manager 같이 쓰려고

public interface WorkerRepository extends JpaRepository<Worker, Long>,CustomRepository{}

위와 같이 CustomRepository 인터페이스를 만들고 구현체 클래스에서 Entity Manager를 주입받고 썼는데 에러가 나서 제가 진행하는 방법이 잘 못 되었는지 질문 드립니다..

답변 4

·

답변을 작성해보세요.

2

차가운물님의 프로필

차가운물

2021.12.15

코드는 강의와 관련된 부분이 아니지만 제가 한 질문은 강의와 관련된 질문인 것 같아서 질문 올려봤습니다.

주제와 벗어났다면 주의하도록 하겠습니다.

 

답변 감사합니다.!!

David님의 프로필

David

2021.12.15

이해해주셔서 감사합니다.

2

David님의 프로필

David

2021.12.14

안녕하세요. Jeongmin Lee님, 공식 서포터즈 David입니다.

.
자세한 코드와 오류 내용을 함께 올려주세요
.
감사합니다.

0

차가운물님의 프로필

차가운물

2021.12.15

안녕하세요 답글이 늦었습니다.

먼저 도움 주셔서 감사합니다.

 

소스코드

https://drive.google.com/file/d/1nlV_PJWleiY05kyEMMkkGV_zqpENjrbM/view?usp=sharing

 

실행방법 :

mysql DB를 준비해 주시고

application.yml 파일에 datasource를 넣어주세요.

그리고 main문을 실행하시면 됩니다.

 

2.문제를 확인하는 법

메인 문을 실행하시면 바로 에러가 나옵니다.

 

제가 질문드리는 것은 JPARepository와 EntityManager를 함께 쓸 수 있는 방법 입니다.

프로젝트에서 WorkerRepository에 가시면 CustomRepository를 상속한 것을 볼 수 있을 것 입니다.

CustomRepository를 구현한 구현체 CustomRepositoryImp에 EntityManager를 주입받고 em을 쓰려고 했지만 잘 안되는 상태입니다.

 

감사합니다.

David님의 프로필

David

2021.12.15

코드를 살펴보았는데 강의와  관련된 내용이 아니시네요.

최대한 강의와 관련된 내용으로 질문 부탁드립니다.

 

WorkerService에 WorkerRepository를 주입받아야 하는데 그러지 못하고 있습니다.

WorkerRepository 타입의 빈을 생성하는 부분에서 문제가 발생한 것 같습니다.

WorkerRepository가 CustomRepository를 상속받고 있는데 CustomRepository 내 정의된 customMethod와 관련된 쿼리를 생성하지 못해서 빈 생성에 실패하는걸로 보입니다.

이 부분을 해결하시면 다음 스텝으로 진행될 것 같습니다.

0

차가운물님의 프로필

차가운물

2021.12.14

넵 

 

public interface WorkerRepository extends JpaRepository<Worker, Long>,CustomRepository{

    Worker findByName(String name);

}
public interface CustomRepository {
    void customMethod();
}
@Service
public class CustomRepositoryImp implements CustomRepository{

    @Autowired
    private EntityManager em;

    @Override
    public void customMethod() {
        System.out.println("test");
    }

}

 

에러 메세지

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'workerService' defined in file [C:\Users\admin\git\rebuild\build\classes\java\main\com\rebuild\service\WorkerService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'workerRepository' defined in com.rebuild.repository.WorkerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.rebuild.repository.CustomRepository.customMethod()! Reason: Failed to create query for method public abstract void com.rebuild.repository.CustomRepository.customMethod()! No property customMethod found for type Worker!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract void com.rebuild.repository.CustomRepository.customMethod()! No property customMethod found for type Worker!

 

어플리케이션이 실행하되지 전에 에러나 나는 상태 입니다.

David님의 프로필

David

2021.12.14

에러는 workService, workRepository가 생성될 때 발생했는데 관련 코드는 없으시네요.


전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.
구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 권한 문제 꼭 확인해주세요

추가로 다음 내용도 코멘트 부탁드립니다.

1. 실행 방법을 알려주세요.
2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.