JPA 查询 - class 错误中没有合适的构造函数
JPA Query - no appropriate constructor in class error
我有一个 Employee 实体 class,它有很多列。我想从这个 class 中获取一些列,因此我使用了 dtos。我创建了一个新的 BaseEmployee class 并在 EmployeeRepository 中编写了查询。但是当我尝试 运行 app.
时出现此错误:“class 错误中没有合适的构造函数”
@Entity
@Table(name = "employees")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "emp_no")
private int id;
@Column(name = "birth_date")
private Date birthDate;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "gender")
private char gender;
@Column(name = "hire_date")
private Date hireDate;
@OneToMany(mappedBy = "employees")
private List<Title> titles;
@OneToMany(mappedBy = "employees")
private List<Salary> salary;
@OneToMany(mappedBy = "employee")
private List<DeptEmp> departmentList;
@OneToMany(mappedBy = "employee")
private List<DeptManager> managerDepartment;
}
我的 dto class:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseEmployee {
private int id;
private Date birthDate;
private String firstName;
private String lastName;
private char gender;
private Date hireDate;
}
Jpa 存储库:
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
List<Employee> getByFirstNameContains(String firstName);
List<Employee> getByFirstNameStartsWith(String firstName);
@Query("Select new dev.serhat.employeeapi.models.dtos.BaseEmployee"
+ "(e.id, e.birthDate, e.firstName, e.lastName, e.gender, e.hireDate) From Employee e WHERE e.id = :id")
Optional<BaseEmployee> getBaseEmployeeById(int id);
}
错误:
HH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2022-02-20 16:33:42.533 INFO 24371 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-02-20 16:33:42.559 INFO 24371 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-02-20 16:33:43.431 ERROR 24371 --- [ restartedMain] o.h.hql.internal.ast.ErrorTracker : Unable to locate appropriate constructor on class [dev.serhat.employeeapi.models.dtos.BaseEmployee]. Expected arguments are: int, java.util.Date, java.lang.String, java.lang.String, char, java.util.Date
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: dev.serhat.employeeapi.models.dtos.BaseEmployee]
2022-02-20 16:33:43.443 ERROR 24371 --- [ restartedMain] o.h.hql.internal.ast.ErrorTracker : Unable to locate appropriate constructor on class [dev.serhat.employeeapi.models.dtos.BaseEmployee]. Expected arguments are: int, java.util.Date, java.lang.String, java.lang.String, char, java.util.Date
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: dev.serhat.employeeapi.models.dtos.BaseEmployee]
org.hibernate.hql.internal.ast.DetailedSemanticException: Unable to locate appropriate constructor on class [dev.serhat.employeeapi.models.dtos.BaseEmployee]. Expected arguments are: int, java.util.Date, java.lang.String, java.lang.String, char, java.util.Date
at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:182) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
错误非常简单,您的 class BaseEmployee
缺少执行 getBaseEmployeeById()
所需的适当构造函数。您的 class 带有 @NoArgsConstructor
和 @AllArgsConstructor
注释,但这些 lombok
注释可能无法被 Spring 识别。您的方法 getBaseEmployeeById()
需要一个具有以下签名的明确定义的构造函数:
public BaseEmployee(int id, Date birthDate, String firstName, String lastName, char gender, Date hireDate)
目前缺少这样的构造函数。
请向您的 class 添加具有所述签名的构造函数,然后重试。
Lombok 生成的 all-args 构造函数调用两个类型为 java.util.Date
的参数。
当 hibernate 执行您的 jpql 时,它在构造函数中使用 org.hibernate.type.TimestampType
个对象,而不是 java.util.Date
个对象。当然没有生成这样的构造函数,因此出现错误。
下面是关于如何处理这种情况的堆栈溢出讨论:How to force Hibernate to return dates as java.util.Date instead of Timestamp?
我有一个 Employee 实体 class,它有很多列。我想从这个 class 中获取一些列,因此我使用了 dtos。我创建了一个新的 BaseEmployee class 并在 EmployeeRepository 中编写了查询。但是当我尝试 运行 app.
时出现此错误:“class 错误中没有合适的构造函数”@Entity
@Table(name = "employees")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "emp_no")
private int id;
@Column(name = "birth_date")
private Date birthDate;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "gender")
private char gender;
@Column(name = "hire_date")
private Date hireDate;
@OneToMany(mappedBy = "employees")
private List<Title> titles;
@OneToMany(mappedBy = "employees")
private List<Salary> salary;
@OneToMany(mappedBy = "employee")
private List<DeptEmp> departmentList;
@OneToMany(mappedBy = "employee")
private List<DeptManager> managerDepartment;
}
我的 dto class:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseEmployee {
private int id;
private Date birthDate;
private String firstName;
private String lastName;
private char gender;
private Date hireDate;
}
Jpa 存储库:
public interface EmployeeRepository extends JpaRepository<Employee,Integer> {
List<Employee> getByFirstNameContains(String firstName);
List<Employee> getByFirstNameStartsWith(String firstName);
@Query("Select new dev.serhat.employeeapi.models.dtos.BaseEmployee"
+ "(e.id, e.birthDate, e.firstName, e.lastName, e.gender, e.hireDate) From Employee e WHERE e.id = :id")
Optional<BaseEmployee> getBaseEmployeeById(int id);
}
错误:
HH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2022-02-20 16:33:42.533 INFO 24371 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-02-20 16:33:42.559 INFO 24371 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-02-20 16:33:43.431 ERROR 24371 --- [ restartedMain] o.h.hql.internal.ast.ErrorTracker : Unable to locate appropriate constructor on class [dev.serhat.employeeapi.models.dtos.BaseEmployee]. Expected arguments are: int, java.util.Date, java.lang.String, java.lang.String, char, java.util.Date
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: dev.serhat.employeeapi.models.dtos.BaseEmployee]
2022-02-20 16:33:43.443 ERROR 24371 --- [ restartedMain] o.h.hql.internal.ast.ErrorTracker : Unable to locate appropriate constructor on class [dev.serhat.employeeapi.models.dtos.BaseEmployee]. Expected arguments are: int, java.util.Date, java.lang.String, java.lang.String, char, java.util.Date
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: dev.serhat.employeeapi.models.dtos.BaseEmployee]
org.hibernate.hql.internal.ast.DetailedSemanticException: Unable to locate appropriate constructor on class [dev.serhat.employeeapi.models.dtos.BaseEmployee]. Expected arguments are: int, java.util.Date, java.lang.String, java.lang.String, char, java.util.Date
at org.hibernate.hql.internal.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:182) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
错误非常简单,您的 class BaseEmployee
缺少执行 getBaseEmployeeById()
所需的适当构造函数。您的 class 带有 @NoArgsConstructor
和 @AllArgsConstructor
注释,但这些 lombok
注释可能无法被 Spring 识别。您的方法 getBaseEmployeeById()
需要一个具有以下签名的明确定义的构造函数:
public BaseEmployee(int id, Date birthDate, String firstName, String lastName, char gender, Date hireDate)
目前缺少这样的构造函数。
请向您的 class 添加具有所述签名的构造函数,然后重试。
Lombok 生成的 all-args 构造函数调用两个类型为 java.util.Date
的参数。
当 hibernate 执行您的 jpql 时,它在构造函数中使用 org.hibernate.type.TimestampType
个对象,而不是 java.util.Date
个对象。当然没有生成这样的构造函数,因此出现错误。
下面是关于如何处理这种情况的堆栈溢出讨论:How to force Hibernate to return dates as java.util.Date instead of Timestamp?