[오류 기록](spring boot jpa) Failed to create query for method public abstract java.util.List 오류

ran·2023년 4월 29일

오류기록

목록 보기
2/4
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User {

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

    @Column(nullable = false, length = 10, unique = true)
    private int student_Id;

    @Column(nullable = false, length = 20, unique = false)
    private String dept;

    @Column(nullable = false, length = 10, unique = false)
    private String name;

    @Column(length = 50)
    private String password;

    @Builder
    public User(String name, String password, String dept, int student_Id){
        this.name = name;
        this.password = password;
        this.dept = dept;
        this.student_Id = student_Id;
    }
}

교내 프로젝트로 학사 시스템을 구성하던 중 위와 같이 user 엔티티를 만들고 있었다.

public interface UserRepository extends JpaRepository<User, Long> {
    /* Security */
    Optional<User> findByStudent_Id(String student_Id);

    /* 중복 검사> 중복인 경우 true, 중복되지 않은경우 false 리턴 */
    boolean existsByStudent_Id(String student_Id);
}

JPA를 이용하여 find 기능을 추가하기 위해 위와 같이 코드를 작성하고 application 을 run 했다.

이렇게 실행을 하면
Failed to create query for method public abstract java.util.List 오류가 난다.

오류가 난 이유는 바로 카멜케이스를 사용하지 않아서 그렇다.
DB와의 매칭을 하기 위해서는 카멜케이스를 이용해야 하는데, 스네이크 케이스를 사용했기 때문에 DB와 매칭되는 것이 없기 때문에 발생했던 오류였다.

따라서 전부다 카멜케이스로 변경해줌으로써 해결할 수 있었다.

  • 수정된 User
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User {

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

    @Column(nullable = false, length = 10, unique = true)
    private int studentId;

    @Column(nullable = false, length = 20, unique = false)
    private String dept;

    @Column(nullable = false, length = 10, unique = false)
    private String name;

    @OneToMany(mappedBy = "user")
    private List<Member> members = new ArrayList<>();

    @Column(length = 50)
    private String password;

    @Builder
    public User(String name, String password, String dept, int studentId){
        this.name = name;
        this.password = password;
        this.dept = dept;
        this.studentId = studentId;
    }
}
  • 수정된 UserRepository
public interface UserRepository extends JpaRepository<User, Long> {
    /* Security */
    Optional<User> findByStudentId(String studentId);

    /* 중복 검사> 중복인 경우 true, 중복되지 않은경우 false 리턴 */
    boolean existsByStudentId(String studentId);
}
profile
Backend Developer

0개의 댓글