Spring 数据 neo4j 自定义 @QueryResult 不识别枚举
Spring data neo4j custom @QueryResult doesn't recognize enums
我正在尝试使用来自不同节点的多个字段创建自定义 @QueryResult
,但查询结果机制似乎无法正确映射枚举。
这是我为这个场景做的一个例子。我创建了一个基本枚举:
public enum MyEnum{
SOMETHING, SOMETHING_ELSE
}
与spring数据neo4j仓库方法同查询:
@Query("Match (people:People)-[:LIVES_IN]->(country:Country) " +
"RETURN people.enum")
List<WithEnumQueryResult> findPeople();
当我触发它时抛出异常:
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate pl.degath.WithEnumQueryResult using constructor pl.degath.WithEnumQueryResult(pl.degath.MyEnum) with arguments SOMETHING_ELSE
我通过反复试验发现:
@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)
}
我也试过添加一些@Converter,例如@Convert(EnumStringConverter.class)
我的枚举前面的注释 属性,但没有帮助。
关于如何让我的 QueryResult 识别枚举有什么想法吗?
编辑:
正如已接受答案的评论中所述,枚举似乎需要无参数构造函数,因此我不得不将我的不可变对象更改为:
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@QueryResult
public class WithEnumQueryResult{
private MyEnum enum; //enum is visible now!
}
下面是我的实体 类 和存储库,它们运行良好。
@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
}
存储库方法
@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);
结果:
{
"id": 68,
"name": "Alex",
"age": 24,
"test": "A",
}
我正在尝试使用来自不同节点的多个字段创建自定义 @QueryResult
,但查询结果机制似乎无法正确映射枚举。
这是我为这个场景做的一个例子。我创建了一个基本枚举:
public enum MyEnum{
SOMETHING, SOMETHING_ELSE
}
与spring数据neo4j仓库方法同查询:
@Query("Match (people:People)-[:LIVES_IN]->(country:Country) " +
"RETURN people.enum")
List<WithEnumQueryResult> findPeople();
当我触发它时抛出异常:
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate pl.degath.WithEnumQueryResult using constructor pl.degath.WithEnumQueryResult(pl.degath.MyEnum) with arguments SOMETHING_ELSE
我通过反复试验发现:
@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)
}
我也试过添加一些@Converter,例如@Convert(EnumStringConverter.class)
我的枚举前面的注释 属性,但没有帮助。
关于如何让我的 QueryResult 识别枚举有什么想法吗?
编辑:
正如已接受答案的评论中所述,枚举似乎需要无参数构造函数,因此我不得不将我的不可变对象更改为:
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@QueryResult
public class WithEnumQueryResult{
private MyEnum enum; //enum is visible now!
}
下面是我的实体 类 和存储库,它们运行良好。
@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
}
存储库方法
@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);
结果:
{
"id": 68,
"name": "Alex",
"age": 24,
"test": "A",
}