0

I use jhipster to generate 2 entity Employee and Department

and relationship:

  • Employee.java
 @ManyToOne
    @JsonIgnoreProperties(value = "employees", allowSetters = true)
    private Department department;
  • Department.java
@OneToMany(mappedBy = "department")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Employee> employees = new HashSet<>();

and when I called api/employees , I had:

{
    "id": 1,
    "code": "FU_EMP_DTP726",
    "name": "Concrete system",
    "birthDate": "2020-11-30",
    "address": "Granite frame",
    "phone": "0152104977",
    "salary": 65309.0,
    "department": {
        "id": 1,
        "code": "FU_DE_787778",
        "name": "Tools"
    }
}

but with api/departments , I had:

{
    "id": 1,
    "code": "FU_DE_787778",
    "name": "Tools",
    "employees": null
}

I didn't know why department's employees is null

riQQ
  • 9,878
  • 7
  • 49
  • 66
  • Probably because OneToMany is lazy fetched, have you considered defining an EntityGraph? https://www.baeldung.com/jpa-entity-graph – Gaël Marziou Dec 02 '20 at 17:46

1 Answers1

0

In bidirectional one-to-many relationship case jhipster do not get all ‘child’ form “One” side,because “N+1” problem .

For your purpose you need to be modify the generated code。use EntityGraph modify repository、add a “employees” Set into ” DepartmentDTO“ and Mapper

How to see both sides in one-to-many relationship generated by JHipsterHow to see both sides in one-to-many relationship generated by JHipster

helleye
  • 46
  • 4