英文:
Spring data neo4j custom @QueryResult doesn't recognize enums
问题
我正在尝试创建一个自定义的@QueryResult
,其中包含来自不同节点的多个字段,但似乎查询结果机制无法正确映射枚举。
这是我为这种情况制作的示例。我创建了一个基本的枚举:
public enum MyEnum{
SOMETHING, SOMETHING_ELSE
}
并在Spring Data 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; //我希望拥有这个,但会抛出错误
private final String enum; //这会将我的枚举返回为String(不会抛出错误)
private final People people; //这个在people的属性中具有正确的枚举(不会抛出错误)
}
我还尝试在枚举属性前添加一些@Converter
注解,例如@Convert(EnumStringConverter.class)
,但没有帮助。
对于如何使我的QueryResult识别枚举,有什么想法吗?
编辑:
正如接受答案中的评论所提到的,似乎枚举需要无参构造函数,所以我不得不将我的不可变对象更改为:
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@QueryResult
public class WithEnumQueryResult{
private MyEnum enum; //现在枚举可见!
}
英文:
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.
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:
@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 e.g. @Convert(EnumStringConverter.class)
annotation in front of my enum property, but it didn't help out.
Any ideas on how can I make my QueryResult recognize enums?
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!
}
答案1
得分: 1
以下是我的实体类和存储库,它们完美地运行正常。
@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"
}
英文:
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",
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论