持久性实体不能为空
Persistence Entity must not be null
使用以下代码我收到错误 Persistence entity must not be null。可能是什么错误。
public interface DistrictRepo extends PagingAndSortingRepository<District, Integer> {
@Query(
"select d.districtId, d.districtName from District d where d.districtId in (:districtIds) group by d.districtId"
)
@RestResource(path="byList")
List<Object[]> byList(@Param("districtIds") List<Integer> districtIds);
}
如果您要制作 "search" 方法,请使用此方法:
@Projection(name = "idAndName", types = {District.class})
public interface IdAndName {
Integer getId();
String getName();
}
@RestResource(path="byIds", rel="byIds")
@Query("select d from District d where d.districtId in (:districtIds)")
List<District> findByIds(@Param("ids") Integer... ids);
然后使用这个 url:
http://localhost:8080/api/districts/search/byIds?ids=1,2,3&projection=idAndName
有关 projection
的更多信息
如果您需要使用具有 return DTO 的分组和聚合的复杂查询,您不能使用 "search" 方法。相反,您必须实施 custom controller,例如:
@RepositoryRestController
@RequestMapping("/districts")
public class DistrictController {
@Autoware
private DistrictRepo repo;
@GetMapping("/report")
public ResponseEntity<?> report(@RequestParam(value = "ids") Integer... ids) {
List<Dto> dtos = repo.getDtosByIds(ids);
return ResponseEntity.ok(new Resources<>(dtos));
}
}
其中 Dto
是这样的:
@Data // This is Lombok annotation (https://projectlombok.org/)
@Relation(value = "district", collectionRelation = "districts")
public class Dto {
private final Integer id;
private final String name;
}
像这样的 repo 方法:
public interface DistrictRepo extends PagingAndSortingRepository<District, Integer> {
@Query("select new ...Dto(d.districtId, d.districtName) from District d where d.districtId in (:districtIds) group by d.districtId")
@RestResource(path="byList", rel="byList")
List<Dto> getDtosByIds(@Param("ids") Integer... ids);
}
更多info.
使用以下代码我收到错误 Persistence entity must not be null。可能是什么错误。
public interface DistrictRepo extends PagingAndSortingRepository<District, Integer> {
@Query(
"select d.districtId, d.districtName from District d where d.districtId in (:districtIds) group by d.districtId"
)
@RestResource(path="byList")
List<Object[]> byList(@Param("districtIds") List<Integer> districtIds);
}
如果您要制作 "search" 方法,请使用此方法:
@Projection(name = "idAndName", types = {District.class})
public interface IdAndName {
Integer getId();
String getName();
}
@RestResource(path="byIds", rel="byIds")
@Query("select d from District d where d.districtId in (:districtIds)")
List<District> findByIds(@Param("ids") Integer... ids);
然后使用这个 url:
http://localhost:8080/api/districts/search/byIds?ids=1,2,3&projection=idAndName
有关 projection
的更多信息如果您需要使用具有 return DTO 的分组和聚合的复杂查询,您不能使用 "search" 方法。相反,您必须实施 custom controller,例如:
@RepositoryRestController
@RequestMapping("/districts")
public class DistrictController {
@Autoware
private DistrictRepo repo;
@GetMapping("/report")
public ResponseEntity<?> report(@RequestParam(value = "ids") Integer... ids) {
List<Dto> dtos = repo.getDtosByIds(ids);
return ResponseEntity.ok(new Resources<>(dtos));
}
}
其中 Dto
是这样的:
@Data // This is Lombok annotation (https://projectlombok.org/)
@Relation(value = "district", collectionRelation = "districts")
public class Dto {
private final Integer id;
private final String name;
}
像这样的 repo 方法:
public interface DistrictRepo extends PagingAndSortingRepository<District, Integer> {
@Query("select new ...Dto(d.districtId, d.districtName) from District d where d.districtId in (:districtIds) group by d.districtId")
@RestResource(path="byList", rel="byList")
List<Dto> getDtosByIds(@Param("ids") Integer... ids);
}
更多info.