搜索具有自定义ID的对象 [英] searching for object with custom id

查看:83
本文介绍了搜索具有自定义ID的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的课

I have a class that looks like this

@Data
@NodeEntity
public class StoryCharacter {
    @Index(unique = true)
    private String agnosticId;
    private String name;

    @Relationship(type = "FAMILIAR_WITH")
    private Set<StoryCharacter> acquaintances;
}

我需要一个与默认long ID不相关的自定义ID.因此,我引入了一个字段并将其设置为index.

I needed a custom ID that is not related to the default long id. So I introduced a field and set it as index.

但是如何通过该ID查找对象?

But how to find the object by that id?

我想这样做

session.openSession().load(StoryCharacter.class, "custom_id")

,但失败,并显示错误消息,提示它必须为Long.我认为也许我需要使用Filter对象通过该ID进行搜索.还是有另一种方法?

but it fails with error that it must be Long. I assume that maybe I need to use Filter object for search by that id. Or is there another way?

推荐答案

如果要使用自定义ID,则必须使用@Id而不是@Index(unique=true)对该字段进行注释.如果您不想手动设置ID,则可以选择提供ID生成策略(更多详细信息文档中的.

If you want to use a custom id the field has to be annotated with @Id instead of @Index(unique=true). In cases you do not want to set the id manually, there is an option to provide a id generation strategy (more details in the documentation.

您看到此错误,因为Neo4j-OGM无法确定您的id字段具有哪种类型,并回退到标准Long.如果您如上所述定义ID,则load将起作用.

You are seeing this error because Neo4j-OGM cannot determine what type your id field has and falls back to the standard Long. If you define your id as mentioned above, the load will work.

这篇关于搜索具有自定义ID的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆