【问题标题】:Spring Data Rest: JOIN search by parent column is throwing error (o.s.d.r.w.RepositoryRestExceptionHandler : PersistentEntity must not be null!)Spring Data Rest: JOIN search by parent column 抛出错误(o.s.d.r.w.RepositoryRestExceptionHandler : PersistentEntity 不能为空!)
【发布时间】:2019-07-01 05:36:13
【问题描述】:

我将 Spring Data Rest 用于如下所示的部门-员工关系,并在 Employee 上定义了 RepositoryRestResource。

class Department {
  @Id
  UUID Id;

  String name;

  @OneToMany(mappedBy = "employee", targetEntity = Employee.class, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
  @RestResource(exported = false)
  private Set<Employee> employees;

}


class Employee {
  @Id
  UUID Id;

  String firstName;

  String lastName;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "dept_ref")
  @RestResource(exported = false)
  private Department department;
}

@Projection(name = "employeeProjection", types = {Employee.class})
interface EmployeeProjection {
  String getFirstName();
  String getLastName();
}


@RepositoryRestResource(excerptProjection = EmployeeProjection.class)
public interface EmployeeRepository extends CrudRepository<Employee, UUID> {

  //this doesn't work
  @Query(value = "SELECT emp.firstName as firstName , emp.lastName as lastName FROM Employee emp JOIN emp.department "
          + "WHERE emp.department.name = :departmentName ")
  List<EmployeeProjection> findByDepartmentName(@Param("departmentName") String departmentName); 


  //this works
  List<EmployeeProjection> findByFirstName(@Param("firstName") String firstName);

}

问题是当我作为 findByDepartmentName 执行联接搜索时,出现以下错误。

2019-07-01 00:26:33.300 ERROR 7396 --- [nio-8443-exec-5] o.s.d.r.w.RepositoryRestExceptionHandler : PersistentEntity must not be null!

java.lang.IllegalArgumentException: PersistentEntity must not be null!
        at org.springframework.util.Assert.notNull(Assert.java:134)
        at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:140)

【问题讨论】:

    标签: spring-data-rest


    【解决方案1】:

    通过将返回类型从List&lt;EmployeeProjection&gt; 更改为List&lt;Employee&gt; 解决了这个问题,它仍然将投影作为excerptProjection 返回给我。不知道为什么 Spring Data Rest 期望这样做,但它可以工作。

    List<Employee> findByDepartmentName(@Param("departmentName") String departmentName); 
    

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2017-10-15
      • 2016-08-20
      相关资源
      最近更新 更多