【发布时间】:2020-12-08 21:32:23
【问题描述】:
我的Java 项目中有以下 3 个Hibernate 实体:
公司状态
@Entity(name = "company_status")
@Table(name = "company_status")
public class CompanyStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@JsonProperty
@Column(name = "company_status_id")
private Integer companyStatusId;
@JsonProperty
@Column(name = "company_status_label")
private String companyStatusLabel;
}
员工状态
@Entity(name = "employee_status")
@Table(name = "employee_status")
public class EmployeeStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty
@Column(name = "employee_status_id")
private Integer employeeStatusId;
@JsonProperty
@Column(name = "employee_status_name")
private String employeeStatusName;
// many other fields
}
CompanyStatusEmployeeStatus(链接两个实体的实体 - 一对一关系)
@Entity(name = "company_status_employee_status")
@Table(name = "company_status_employee_status")
public class CompanyStatusEmployeeStatus implements Serializable {
// int(20)
@Id
@JsonProperty
@Column(name = "company_status_id")
private Integer companyStatusId;
// int(20)
@JsonProperty
@Column(name = "employee_status_id")
private Integer employeeStatusId;
}
我只想将 JSON 响应中的必要字段返回到前端,因此我创建了一个较小的 CompanyStatusDTO 对象,该对象还嵌套了一个 EmployeeStatusDTO 列表
CompanyStatusDTO
public class CompanyStatusDTO {
@JsonProperty
private Integer companyStatusId;
@JsonProperty
private String companyStatusLabel;
@JsonProperty
private List <EmployeeStatusDTO> employeeStatusDTOs;
}
EmployeeStatusDTO
public class EmployeeStatusDTO {
@JsonProperty
private Integer employeeStatusId;
@JsonProperty
private String employeeStatusName;
}
但是,我对使用 Hibernate 比较陌生 - 有没有一种方法可以创建一个查询,将结果直接从我的 MySQL DB 映射到我的 CompanyStatusDTOobject?
如果是这样,我该怎么做?
【问题讨论】:
标签: java mysql json hibernate dto