Spring data jpa -如何在子节点上进行选择并输出所有选中子节点的父节点?

nvbavucw  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(157)

我有那些实体

@Entity
@Data
@Table(name = "CITY")
public class CityEntity {
    @Id private int id;
    private String name;
    private String postcode;
    @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name="ID_CITY_FK") 
    private List<SchoolEntity> schools;
}

字符串

@Entity
@Data
@Table(name = "SCHOOL")
public class SchoolEntity {
    @Id private int id;
    private String name;
    @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="ID_CITY_FK") 
    private CityEntity city;
}


我想根据学校名称进行选择,并输出如下所示

[
  {
    "id": 1,
    "name": "Paris",
    "postcode": "75000",
    "schools": [
      {
        "id": 10,
        "name": "Java School 1"
      },
      {
        "id": 11,
        "name": "Java School 2"
      }
    ]
  },
  {
    "id": 2,
    "name": "Brussels",
    "postcode": "1000",
    "schools": [
      {
        "id": 12,
        "name": "Java School 3"
      },
      {
        "id": 13,
        "name": "Java School 4"
      }
    ]
  }
]


我认为唯一的办法是从学校实体开始

public interface SchoolEntityRepository extends JpaRepository<SchoolEntity, Integer> {
    List<SchoolEntity> findByNameContaining(String name);
}


然后用我的控制器我就可以检索学校

@GetMapping("/from-school")
public List<City> fromSchool2() {
    List<SchoolEntity> schoolEntities = schoolEntityRepository.findByNameContaining("Java");
    return ...;
}


我也是一个MapstructMap器

@Mapper
public interface CityEntityMapper {
    CityEntityMapper INSTANCE = Mappers.getMapper(CityEntityMapper.class);
    List<City> map(List<SchoolEntity> schools);
}


但是...?我不知道该怎么做?

unhi4e5o

unhi4e5o1#

使用SchoolEntity Repository根据学校名称获取SchoolEntity示例。

public interface SchoolEntityRepository extends JpaRepository<SchoolEntity, Integer> {
    List<SchoolEntity> findByNameContaining(String name);
}

字符串

按关联的CityEntity对学校进行分组

@GetMapping("/from-school")
public List<City> fromSchool2() {
    List<SchoolEntity> schoolEntities = schoolEntityRepository.findByNameContaining("Java");

    // Group schools by city
    Map<CityEntity, List<SchoolEntity>> schoolsByCity = schoolEntities.stream()
            .collect(Collectors.groupingBy(SchoolEntity::getCity));

    // Convert the map entries to City DTOs
    return schoolsByCity.entrySet().stream()
            .map(entry -> CityEntityMapper.INSTANCE.cityEntityToCity(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());
}

使用MapStruct将结果数据结构Map到所需的DTO结构

@Mapper
public interface CityEntityMapper {
    CityEntityMapper INSTANCE = Mappers.getMapper(CityEntityMapper.class);

    @Mapping(source = "city.name", target = "name")
    @Mapping(source = "city.postcode", target = "postcode")
    @Mapping(source = "schools", target = "schools")
    City cityEntityToCity(CityEntity city, List<SchoolEntity> schools);
}


DTO:

@Data
public class City {
    private int id;
    private String name;
    private String postcode;
    private List<School> schools;
}

@Data
public class School {
    private int id;
    private String name;
}

相关问题