简体   繁体   English

如何仅从关系中检索属性而不是实体?

[英]How can I retrieve only an attribute instead of the Entity from a relationship?

I'm working on a maven web application project in Netbeans and I have the following classes: 我正在Netbeans中从事Maven Web应用程序项目,我有以下课程:

@Entity
@Table(name = "departments")
public class Department{
    @Id
    private Integer departmentId;
    @Column(name = "department_name")
    private String departmentName;
}

And: 和:

@Entity
@Table(name = "employees")
public class Employee {
    @Id
    private Integer employeetId;
    @Column(name = "employee_name")
    private String employeeName;
    @JoinColumn(name = "department_id", referencedColumnName = 
    "department_id")
    @ManyToOne(optional = false)
    private Departments department;
}

My rest api return this: 我的休息API返回此:

{
    employeeId:1, 
    employeeName:"Jhon",
    department: { departmentId:1, departmentName:"IT"}
}

My desired output is: 我想要的输出是:

 {
    employeeId:1, 
    employeeName:"Jhon",
    department: "IT"
 }

I try to return an DTO, but I get an empty json: 我尝试返回DTO,但得到一个空的json:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<EmployeeDto> findAllEmployees() {
    CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery(Employee.class);
    cq.select(cq.from(Employee.class));
    List<Employee> employees = entityManager.createQuery(cq).getResultList();
    List<EmployeeDto> employeesDto = new ArrayList<>();

    for (Employee employee : employees) {
        EmployeeDto employeeDto = new EmployeeDto();
        employeeDto.employeeId = employee.getEmployeedId();
        employeeDto.department = employee.getDepartment().getDepartmentName();

        employeesDto.add(employeeDto);
    }
    return   employeesDto;

DTO: DTO:

Class EmployeeDto{Integer employeeId; String employeeName; String department}

Using NamedQueries 使用命名查询

@Entity
@Table(name="employees")
@NamedQueries({
    @NamedQuery(
        name = "getDesiredUser",
        query = "SELECT e.employeeId, e.employeeName, d.departmentName FROM employees AS e INNER JOIN departments as d ON e.department_id = d.departmentId"),

})
public class Employee {
    @Id
..
}

You can find more on NamedQueries : 您可以在NamedQueries上找到更多信息:

https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-creating-database-queries-with-named-queries/ https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-creating-database-queries-with-named-queries/

This is because I get an empty json: My Dto class has not public getters and setters. 这是因为我得到了一个空的json:我的Dto类没有公共的获取器和设置器。 The solution is to make the Dto fields public or add public getters/setters. 解决方案是公开Dto字段或添加公共获取者/设置者。

Class EmployeeDto{
    public Integer employeeId; 
    public String employeeName; 
    public String department;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当实体中存在关系时,我可以只使用实体的 id 而不是从数据库中获取实体吗? - Can I use only Entity's id instead of fetching an entity from DB when there is a relationship in an Entity? 如何仅检索ID而不是关联的实体? - How do I retrieve only the ID instead of the Entity of an association? 休眠:如何从“一个”侧删除“许多”侧中一对多关系中的实体 - Hibernate: how can I delete an entity in the one-to-many relationship in the “many” side from the “one” side 如何将实体作为属性传递给另一个实体? - How can I pass an entity as a attribute to another entity? 我怎样才能只检索日期? Java - How can I only retrieve the date? Java 如何从数据库(java)读取的字符串中为实体设置枚举属性? - How can I set an enum attribute in my entity from a string read from a database (java)? 如何从 firebase 结构而不是 Boolean 值中检索字段? - How can i retrieve the fields from the firebase structure instead of the Boolean values? 如何使用 Hibernate 更新实体而不是将其插入数据库? - How can I update an entity instead of inserting it into database using Hibernate? JaxB:如何从嵌套元素中检索文本属性? - JaxB: How Do I Retrieve Text Attribute from Nested Element? 如何从 HttpServletRequest 中检索 RequestMappingInfo? - How can I retrieve RequestMappingInfo from HttpServletRequest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM