搜索具有自定义 ID 的对象

searching for object with custom id

我有一个 class 看起来像这样

@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.

但是如何通过那个id找到对象呢?

我想这样做

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

但它失败了,错误是它必须是 Long。我假设也许我需要使用 Filter 对象来按该 ID 进行搜索。或者有别的办法吗?

如果您想使用自定义 ID,则必须使用 @Id 而非 @Index(unique=true) 对字段进行注释。如果您不想手动设置 ID,可以选择提供 ID 生成策略(更多详情 in the documentation.

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