英文:
table name not mapped using jpa
问题
嗨,我正在使用Spring+JPA进行一些CURD操作。根据流程,我已经创建了所有的类和实体类。但是当我运行项目时,我遇到了一个错误,错误信息是**Employee未映射**。
我查看了一些解决方案,他们提到查询实体名应与实体类名相同。我也已经做了这个,但仍然遇到相同的错误。以下是我的代码。
**控制器**
```java
package com.springboot.controller;
// ... 其他导入语句
@RestController
@RequestMapping("/api")
public class EmployeeController {
private EmployeeDao dao;
@Autowired
public EmployeeController(EmployeeDao dao) {
this.dao = dao;
}
@GetMapping("/employees")
public List<Employee> getEmployees() {
return dao.getEmployee();
}
}
Dao接口
package com.springboot.dao;
// ... 其他导入语句
public interface EmployeeDao {
List<Employee> getEmployee();
}
DaoImpl
package com.springboot.daoImpl;
// ... 其他导入语句
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
private EntityManager em;
@Autowired
public EmployeeDaoImpl(EntityManager em) {
this.em = em;
}
@Override
@Transactional
public List<Employee> getEmployee() {
TypedQuery<Employee> query = em.createQuery("from Employee e", Employee.class);
return query.getResultList();
}
}
Employee实体类
package com.springboot.entity;
// ... 其他导入语句
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "emp_seq")
@SequenceGenerator(sequenceName = "emp_seq", allocationSize = 1, initialValue = 1, name = "emp_seq")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
// ... getter和setter方法
}
application.properties
# JDBC属性
spring.datasource.url=jdbc:postgresql://localhost:5432/emp
spring.datasource.username=postgres
spring.datasource.password=root
# Hibernate属性
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.show-sql=true
# spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
错误
org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee e]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee e]"
请有人帮忙解决这个问题。
<details>
<summary>英文:</summary>
Hi i am working on some CURD operation using Spring+JPA. I have created all the class and entity class as per the flow. But when i am running my project i am getting an error saying my **Employee is not mapped**.
I went to few of the solution where they have mention that the query entity name should be same as per your entity class name.I have done that as well but still i am getting the same error.Following is my code.
**Controller**
package com.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.dao.EmployeeDao;
import com.springboot.entity.Employee;
@RestController
@RequestMapping("/api")
public class EmployeeController {
private EmployeeDao dao;
@Autowired
public EmployeeController(EmployeeDao dao) {
this.dao = dao;
}
@GetMapping("/employees")
public List<Employee> getEmployees() {
return dao.getEmployee();
}
}
**Dao Interface**
package com.springboot.dao;
import java.util.List;
import com.springboot.entity.Employee;
public interface EmployeeDao {
public List<Employee> getEmployee();
}
**DaoImpl**
package com.springboot.daoImpl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.springboot.dao.EmployeeDao;
import com.springboot.entity.Employee;
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
private EntityManager em;
@Autowired
public EmployeeDaoImpl(EntityManager em) {
this.em = em;
}
@Override
@Transactional
public List<Employee> getEmployee() {
TypedQuery<Employee> query = em.createQuery("from Employee e",Employee.class);
return query.getResultList();
}
}
**Employee Entity Class**
package com.springboot.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "emp_seq")
@SequenceGenerator(sequenceName = "emp_seq",allocationSize = 1,initialValue = 1, name = "emp_seq")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String 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;
}
}
**Application.property**
#JDBC Property
spring.datasource.url=jdbc:postgresql://localhost:5432/emp
spring.datasource.username=postgres
spring.datasource.password=root
Hibernate properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.show-sql=true
#spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
**Error**
org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee e]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee e]"
Can someone please help me on this
</details>
# 答案1
**得分**: 0
你忘记了 `@ResponseBody` 注解。
```java
@GetMapping("/employees")
public @ResponseBody List<Employee> getEmployees() {
return dao.getEmployee();
}
还要记住,直接返回列表并不是一个好的做法。最好将列表封装到某个类中,以确保如果将来需要添加一些字段,比如 "description" 或者其他任何字段,都能够轻松实现。
英文:
You forgot @ResponseBody
annotation.
@GetMapping("/employees")
public @ResponseBody List<Employee> getEmployees() {
return dao.getEmployee();
}
Also keep in mind that, it is not a good practice to return a list. You better wrap your list into some class, in order to make sure that if in future, you will have to add some field like "description" or any other(doesn't really matter what field), you will be able to do it.
答案2
得分: 0
尝试将“from Employee e”更改为“from Employee”。
英文:
Try to change "from Employee e" -> "from Employee".
专注分享java语言的经验与见解,让所有开发者获益!
评论