我的实体Citizen
有一个WeeklyCare
的列表。每个WeeklyCare
都有一个SubCategoryCare
的列表。我有一个使用CrudRepository
获取所有公民的Web服务。我想要的是从每个SubCategoryCare
的每个WeeklyCare
中排除Citizen
。有人告诉我可以使用EntityGraph
来完成此操作,但是我无法使其正常工作。
这是我的Citizen
和EntityGraph
:
@Entity
@Table(name = "citizens")
@NamedEntityGraph(
name = "Citizen.noSubCategoryCare",
attributeNodes = {
@NamedAttributeNode("cpr"),
@NamedAttributeNode("name"),
@NamedAttributeNode("section"),
@NamedAttributeNode(value = "weeklyCare", subgraph = "weeklyCareSubgraph"),
},
subgraphs = {
@NamedSubgraph(
name = "weeklyCareSubgraph",
attributeNodes = {
@NamedAttributeNode("id"),
@NamedAttributeNode("week"),
@NamedAttributeNode("year"),
@NamedAttributeNode("practicalCare"),
@NamedAttributeNode("personalCare"),
@NamedAttributeNode("healthCare"),
@NamedAttributeNode("rehabilitation"),
@NamedAttributeNode("totalCare"),
@NamedAttributeNode("hospitalized"),
@NamedAttributeNode("citizenId"),
}
)
}
)
public class Citizen {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NaturalId
@Size(max = 10, min = 10, message = "CPR must be exactly 10 characters")
private String cpr;
@NotNull
private String name;
@NotNull
private String section;
@OneToMany(mappedBy = "citizen", cascade = CascadeType.ALL, orphanRemoval = true)
private List<WeeklyCare> weeklyCare;
}
这是我的CrudRepository
:
public interface CitizenRepository extends CrudRepository<Citizen, Long>{
@EntityGraph(value = "Citizen.noSubCategoryCare", type = EntityGraph.EntityGraphType.FETCH)
@Query("SELECT c FROM Citizen c")
List<Citizen> findAllWithoutSubCategoryCare();
@EntityGraph(value = "Citizen.noSubCategoryCare", type = EntityGraph.EntityGraphType.FETCH)
Citizen findByCpr(String cpr);
}
在返回公民的Controller
中,我正在执行以下操作:
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping(path = "/all") // Map ONLY GET Requests
public @ResponseBody
Iterable<Citizen> getAllCitizens() {
Citizen c = citizenRepository.findByCpr("someCpr");
Iterable<Citizen> cc2 = citizenRepository.findAllWithoutSubCategoryCare();
return cc2;
}
两种方法都返回包含每个属性的公民。我在做什么错了?