简体   繁体   English

Spring数据neo4j自定义@QueryResult无法识别枚举

[英]Spring data neo4j custom @QueryResult doesn't recognize enums

I'm trying to create a custom @QueryResult with multiple fields from different nodes, but it seems like the query result mechanism is unable to map enum correctly.我正在尝试使用来自不同节点的多个字段创建自定义@QueryResult ,但似乎查询结果机制无法正确映射枚举。

This is an example that I made for this scenario.这是我为此场景制作的示例。 I created a basic enum as:我创建了一个基本的枚举:

public enum MyEnum{
    SOMETHING, SOMETHING_ELSE
}

And spring data neo4j repository method with the query:以及带有查询的 spring 数据 neo4j 存储库方法:

    @Query("Match (people:People)-[:LIVES_IN]->(country:Country) " +
            "RETURN people.enum")
    List<WithEnumQueryResult> findPeople();

when I trigger it throws an exception:当我触发它时抛出异常:

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate pl.degath.WithEnumQueryResult using constructor pl.degath.WithEnumQueryResult(pl.degath.MyEnum) with arguments SOMETHING_ELSE

I was able to see through trial and error that:我能够通过反复试验看到:

@Builder
@Getter
@QueryResult
public class WithEnumQueryResult{
    private final MyEnum enum; //this one I would like to have, but throws error
    private final String enum; //this returns my enum as String (doesn't throw error)
    private final People people; //this one has correct enum as a property of people (doesn't throw error)
}

I tried also add some @Converter eg @Convert(EnumStringConverter.class) annotation in front of my enum property, but it didn't help out.我还尝试在我的 enum 属性前面添加一些 @Converter 例如@Convert(EnumStringConverter.class)注释,但它没有帮助。

Any ideas on how can I make my QueryResult recognize enums?关于如何让我的 QueryResult 识别枚举的任何想法?

EDIT:编辑:

As mentioned in a comment from the accepted answer, it seems like enums require no-args constructors, so I had to change my immutable object into the:正如在接受的答案的评论中提到的,枚举似乎需要无参数构造函数,所以我不得不将我的不可变对象更改为:

@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@QueryResult
public class WithEnumQueryResult{
    private MyEnum enum; //enum is visible now!
}

Below are my entity classes and repository which perfectly works fine.下面是我的实体类和存储库,它们完美地工作。

@Data
@QueryResult
public class PersonResponse {
    private Long id;
    private String name;
    private int age;
    private City livesAt;
    private Test test;
    private List<Person> friends;
}
public enum Test {
    A, B
}

Repository method存储库方法

 @Query("MATCH (pr:Person) where ID(pr)=$id return ID(pr) as id, pr.test as test, pr.name as name, pr.age as age")
    public PersonResponse getPerson(Long id);

Result:结果:

{
  "id": 68,
  "name": "Alex",
  "age": 24,
  "test": "A",
  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM