持久性实体不能为空 [英] Persistence Entity must not be null
本文介绍了持久性实体不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码我收到错误持久性实体不能为空.可能是什么错误.
with following code I am getting error Persistence entity must not be null. what could be the mistake.
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);
}
推荐答案
如果您打算采用搜索"方法,请使用以下方法:
If you are going to make a "search" method, use this approach:
@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);
然后使用这个网址:
http://localhost:8080/api/districts/search/byIds?ids=1,2,3&projection=idAndName
关于投影的更多信息
如果您需要使用带有返回 DTO 的分组和聚合的复杂查询,则不能使用搜索"方法.相反,您必须实施 自定义控制器,例如:
If you need to use complex queries with grouping and aggregation that return DTOs you can not use "search" methods. Instead you have to implement custom controller, for example:
@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 方法:
And something like this the repo method:
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);
}
更多信息.
这篇关于持久性实体不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文