我有一個 Employee 物體類,它有很多列。我想從這個類中獲取一些列,因此我使用了 dtos。我創建了一個新的 BaseEmployee 類并在 EmployeeRepository 中撰寫了查詢。但是當我嘗試運行 app.
@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 課程:
@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]
uj5u.com熱心網友回復:
Lombok 生成的全引數建構式呼叫兩個型別的引數java.util.Date。
當 hibernate 執行你的 jpql 時,它org.hibernate.type.TimestampType在建構式中使用物件而不是java.util.Date物件。當然沒有生成這樣的建構式,因此出現了錯誤。
下面是關于如何處理這種情況的堆疊溢位討論:How to force Hibernate to return dates as java.util.Date 而不是 Timestamp?
uj5u.com熱心網友回復:
該錯誤非常簡單,您的類BaseEmployee缺少執行所需的適當建構式getBaseEmployeeById()。您的類帶有注釋@NoArgsConstructor,@AllArgsConstructor但這些lombok注釋可能無法被 Spring 識別。您的方法getBaseEmployeeById()需要具有以下簽名的顯式定義的建構式:
public BaseEmployee(int id, Date birthDate, String firstName, String lastName, char gender, Date hireDate)
目前缺少這樣的建構式。
請向您的類添加具有所述簽名的建構式,然后重試。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429908.html
