小编Osk*_*ica的帖子

自定义查询不适用于 Spring Data Neo4j 存储库中的接口投影

为了排除实体的一些字段,我创建了一个接口投影,它仅返回一些字段。

我想在存储库接口(扩展Neo4jRepository)中添加一个方法,该方法使用用 Cypher 编写的自定义查询(使用@Query注释)

如果我将返回对象设置为实体,则此方法有效,但当null返回对象设置为投影时,该方法会返回。投影与普通存储库方法一起使用(无需自定义查询)

示例代码: 另外 - 我使用 lombok 但我怀疑它在这里有什么区别

PersonEntity.java

@NodeEntity
@Data
public class Person {
  @Id
  @GeneratedValue
  private Long id;
  @Property("first_name")
  private String firstName;
  @Property("last_name")
  private String lastName;
  @Property("is_man")
  private boolean isMan;
Run Code Online (Sandbox Code Playgroud)

PersonProjection.java

public interface PersonProjection {
  Long getId();
  String getFirstName();
  boolean getIsMan();
}
Run Code Online (Sandbox Code Playgroud)

PersonRepository.java

public interface PersonRepository extends Neo4jRepository<Person, Long> {
  @Query("MATCH (n:`Person`) WHERE n.`is_man` = true WITH n RETURN n")
  List<PersonProjection> findMen(); // doesn't work, returns null

  List<PersonProjection> findAllByIsManTrue(); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data spring-data-neo4j spring-boot

5
推荐指数
1
解决办法
837
查看次数