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

jay님의 프로필 이미지
jay

작성한 질문수

실전! 스프링 데이터 JPA

메소드 이름으로 쿼리 생성

MemberRepositoryTest를 돌리는 과정에서 아래와 같이 에러가 나타납니다.

작성

·

1.7K

0

6:40초 부분 빌드를 하는 과정에서 아래와 같은 에러를 만났습니다.

Error creating bean with name 'memberRepository' defined in study.datajpa.repository.MemberRepository 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 java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! Reason: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?

 

 

 

Error creating bean with name 'memberRepository' defined in study.datajpa.repository.MemberRepository 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 java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! Reason: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?

 

 

2022-04-16 00:51:55.562 ERROR 20525 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@7e276594] to prepare test instance [study.datajpa.repository.MemberRepositoryTest@2148b47e]

 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2022-04-16 00:51:55.558 ERROR 20525 --- [           main] o.s.boot.SpringApplication               : Application run failed

 

 

Error creating bean with name 'memberRepository' defined in study.datajpa.repository.MemberRepository 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 java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! Reason: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?

 

 

MemberRepositoryTest 입니다.

package study.datajpa.repository;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import study.datajpa.entity.Member;

import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
@Transactional
@Rollback(false)
class MemberRepositoryTest {

@Autowired
MemberRepository memberRepository;

@Test
public void testMember() {
System.out.println("memberRepository = " + memberRepository.getClass());
Member member = new Member("memberA");
Member savedMember = memberRepository.save(member);

Member findMember = memberRepository.findById(savedMember.getId()).get();

assertThat(findMember.getId()).isEqualTo(member.getId());
assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
assertThat(findMember).isEqualTo(member);
}


@Test
public void basicCRUD() {
Member member1 = new Member("member1");
Member member2 = new Member("member2");
memberRepository.save(member1);
memberRepository.save(member2);

// 단건 조회 검증
Member findMember1 = memberRepository.findById(member1.getId()).get();
Member findMember2 = memberRepository.findById(member2.getId()).get();
assertThat(findMember1).isEqualTo(member1);
assertThat(findMember2).isEqualTo(member2);

findMember1.setUsername("member!!!!!!!");

// 리스트 조회 검증
List<Member> all = memberRepository.findAll();
assertThat(all.size()).isEqualTo(2);

// 카운트 검증
long count = memberRepository.count();
assertThat(count).isEqualTo(2);

// 삭제 검증
memberRepository.delete(member1);
memberRepository.delete(member2);

long deleteCount = memberRepository.count();
assertThat(deleteCount).isEqualTo(0);

}

@Test
public void findByUserNameAndAgeGreaterThen() {
Member m1 = new Member("AAA", 10);
Member m2 = new Member("AAA", 20);
memberRepository.save(m1);
memberRepository.save(m2);

List<Member> result = memberRepository.findByUserNameAndAgeGreaterThen("AAA", 15);

assertThat(result.get(0).getUsername()).isEqualTo("AAA");
assertThat(result.get(0).getAge()).isEqualTo(20);
assertThat(result.size()).isEqualTo(1);
}

@Test
public void findHelloBy() {
List<Member> helloBy = memberRepository.findTop3HelloBy();
}

}

 

 

MemberRepository  Interface입니다.

package study.datajpa.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import study.datajpa.entity.Member;

import java.util.List;

public interface MemberRepository extends JpaRepository<Member, Long> {

List<Member> findByUserNameAndAgeGreaterThen(String username, int age);

List<Member> findTop3HelloBy();
}

 

아래 글을 참고하여서

error 로그를 구글링했지만 해결을 하지 못하였습니다..

https://heestory217.tistory.com/131

https://solbel.tistory.com/1447

 

다른 부분에서 추가해야할 작업이 있나요??..

 

 

 

답변 2

1

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

Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUserNameAndAgeGreaterThen(java.lang.String,int)! No property 'userName' found for type 'Member'! Did you mean ''username''?;

에러 메시지에서 Member클래스에서 userName의 필드명을 못찾는다고 하네요.

userName이 아니라 username이 아닌가요?

그러면 findByUserName이 아니라 findByUsername으로 작성하셔야 합니다.

감사합니다.

 

0

jay님의 프로필 이미지
jay
질문자

네 감사합니다. 위와 같이 findUsername으로 작성을 하고 돌렸는데 위와 비슷한 에러를 만나서 해결을 계속하고 있지만 찾지못하였습니다..... 

Error creating bean with name 'memberRepository' defined in study.datajpa.repository.MemberRepository 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 java.util.List study.datajpa.repository.MemberRepository.findByUsernameAndAgeGreaterThen(java.lang.String,int)! Reason: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUsernameAndAgeGreaterThen(java.lang.String,int)! No property 'greaterThen' found for type 'int'! Traversed path: Member.age.; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List study.datajpa.repository.MemberRepository.findByUsernameAndAgeGreaterThen(java.lang.String,int)! No property 'greaterThen' found for type 'int'! Traversed path: Member.age.

 

Member Class입니다.

package study.datajpa.entity;


import lombok.*;

import javax.persistence.*;

import static javax.persistence.FetchType.*;

@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "username", "age"})
public class Member {

@Id
@GeneratedValue
@Column(name = "member_id")
private Long id;
private String username;
private int age;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "tema_id")
private Team team;

public Member(String username) {
this.username = username;
}


public Member(String username, int age) {
this.username = username;
this.age = age;
}

public Member(String username, int age, Team team) {
this.username = username;
this.age = age;
if (team != null) {
changeTeam(team);
}
}

public void changeTeam(Team team) {
this.team = team;
team.getMembers().add(this);
}
}


 

MemberRepository 입니다.

package study.datajpa.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import study.datajpa.entity.Member;

import java.util.List;

public interface MemberRepository extends JpaRepository<Member, Long> {

List<Member> findByUsernameAndAgeGreaterThen(String username, int age);

List<Member> findHelloBy();
}

 

MemberRepositoryTest입니다.

package study.datajpa.repository;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import study.datajpa.entity.Member;

import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
@Transactional
@Rollback(false)
class MemberRepositoryTest {

@Autowired
MemberRepository memberRepository;

@Test
public void testMember() {
System.out.println("memberRepository = " + memberRepository.getClass());
Member member = new Member("memberA");
Member savedMember = memberRepository.save(member);

Member findMember = memberRepository.findById(savedMember.getId()).get();

assertThat(findMember.getId()).isEqualTo(member.getId());
assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
assertThat(findMember).isEqualTo(member);
}


@Test
public void basicCRUD() {
Member member1 = new Member("member1");
Member member2 = new Member("member2");
memberRepository.save(member1);
memberRepository.save(member2);

// 단건 조회 검증
Member findMember1 = memberRepository.findById(member1.getId()).get();
Member findMember2 = memberRepository.findById(member2.getId()).get();
assertThat(findMember1).isEqualTo(member1);
assertThat(findMember2).isEqualTo(member2);

findMember1.setUsername("member!!!!!!!");

// 리스트 조회 검증
List<Member> all = memberRepository.findAll();
assertThat(all.size()).isEqualTo(2);

// 카운트 검증
long count = memberRepository.count();
assertThat(count).isEqualTo(2);

// 삭제 검증
memberRepository.delete(member1);
memberRepository.delete(member2);

long deleteCount = memberRepository.count();
assertThat(deleteCount).isEqualTo(0);

}

@Test
public void findByUsernameAndAgeGreaterThen() {
Member m1 = new Member("AAA", 10);
Member m2 = new Member("AAA", 20);
memberRepository.save(m1);
memberRepository.save(m2);

List<Member> result = memberRepository.findByUsernameAndAgeGreaterThen("AAA", 15);

assertThat(result.get(0).getUsername()).isEqualTo("AAA");
assertThat(result.get(0).getAge()).isEqualTo(20);
assertThat(result.size()).isEqualTo(1);
}

@Test
public void findHelloBy() {
List<Member> helloBy = memberRepository.findHelloBy();
}

}

GreaterThen -> GreateThan으로 변경해주세요.

jay님의 프로필 이미지
jay
질문자

답변 감사합니다.

jay님의 프로필 이미지
jay

작성한 질문수

질문하기