-
[Spring-Hibernate] cretaeQuery, is not mapped 에러개발/Spring 2021. 11. 6. 14:16
Hibernate의 경우 SQL query의 대소문자를 실제 Entiry의 Class이름과 필드 이름을 정확히 맞춰줘야 한다.
만약 다음과 같은 Enrity가 있다고 하자.
package com.ukhyun.hibernate.demo.entity; import javax.persistence.*; @Entity @Table(name = "student") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // Auto increment @Column(name = "id") private int id; @Column(name = "first_name") private String firstName; @Column(name = "last_name") private String lastName; @Column(name = "email") private String email; public Student() { } public Student(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Student{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + '}'; } }
그리고 만약 다음과 같은 쿼리를 작성하고 싶다면..
SELECT * FROM STUDENT WHERE LAST_NAME = 'LASTNAME';
List<Student> students = session.createQuery("from Student where lastName='LASTNAME').getResultList() // 테이블 이름은 현재 만들어 놓은 클래스 이름과 같게 Student // 필드 역시도 클래스 내의 필드와 같이 lastName 으로 해줘야함.
로 작성하면 된다.
'개발 > Spring' 카테고리의 다른 글
[Hibernate] One-to-many, 외래키가 null (0) 2021.11.09 [Spring-hibernate] Cascade 종류 (0) 2021.11.06 [Spring] Bean Scope (0) 2021.11.04 [Spring] Autowired (0) 2021.11.03 Spring boot를 하기위해 Spring을 시작하기로 했다. (0) 2021.11.03