努力使用主索引从 Neo4j 中检索实体

Struggling with retrieving an entity from Neo4j using a primary index

我有以下简单的继承层次结构

    abstract class Entity {

        @GraphId
        Long id;

        public Long getId() {
            return id;
        }
    }


    @NodeEntity
    public class Asset extends Entity {

        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Index(unique=true, primary=true)
        String primaryKey;

        public String getPrimaryKey() {
            return primaryKey;
        }

        public void setPrimaryKey(String primaryKey) {
            this.primaryKey = primaryKey;
        }
   }

@NodeEntity
public class Equity extends Asset {

    @Override
    public String toString() {
        return "Equity [name=" + name + ", primaryKey=" + primaryKey + "]";
    }

}

我的存储库实现为:

public class AssetServiceImpl extends GenericService<Asset> implements AssetService {

    @Override
    public Asset findPK(String primaryKey)
    {
        return session.load(getEntityType(), primaryKey, 0);
    }

    @Override
     public Asset create(Asset asset) {

            Asset foundAsset = findPK(asset.getPrimaryKey()); 
            if (foundAsset == null)
            {
                session.save(asset, 0);
                return findPK(asset.getPrimaryKey());
            }
            else
            {
                return foundAsset;
            }
    }

    @Override
    public Class<Asset> getEntityType() {
        return Asset.class;
    }
}

我正在使用简单的应用程序进行测试:

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );        

        AssetService assetService = new AssetServiceImpl();

        Equity equity1 = new Equity();
        equity1.setName("British Airways");
        equity1.setPrimaryKey("PK1");

        assetService.create(equity1);

        System.out.println(assetService.findPK("PK1"));       
        return;     
    }
}

当我 运行 应用程序时,Equity 在数据库中按预期创建,所有属性和标签都如预期的那样表示继承。问题是 findPK 方法只会 return null。所以从一个干净的数据库开始我

日志记录显示约束的创建、公平的创建以及寻找它的尝试。从表面上看一切都很好,但什么也找不到!我将非常感谢有关此的任何提示我确定我错过了一些愚蠢的东西。

Hello World!
[main] INFO org.neo4j.ogm.metadata.ClassFileProcessor - Starting Post-processing phase
[main] INFO org.neo4j.ogm.metadata.ClassFileProcessor - Building annotation class map
[main] INFO org.neo4j.ogm.metadata.ClassFileProcessor - Building interface class map for 14 classes
[main] INFO org.neo4j.ogm.metadata.ClassFileProcessor - Post-processing complete
[main] INFO org.neo4j.ogm.metadata.ClassFileProcessor - 14 classes loaded in 80728985 nanoseconds
[main] INFO org.neo4j.ogm.service.DriverService - Using: [org.neo4j.ogm.drivers.bolt.driver.BoltDriver]
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: CALL db.constraints() with params {}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: CALL db.indexes() with params {}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: DROP CONSTRAINT ON ( asset:Asset ) ASSERT asset.primaryKey IS UNIQUE with params {}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: CREATE CONSTRAINT ON ( asset:Asset ) ASSERT asset.primaryKey IS UNIQUE with params {}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: MATCH (n) WHERE n.primaryKey = { id } RETURN n with params {id=PK1}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: UNWIND {rows} as row CREATE (n:`Equity`:`Asset`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, row.type as type with params {rows=[{nodeRef=-1647809929, type=node, props={name=British Airways, primaryKey=PK1}}]}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: MATCH (n) WHERE n.primaryKey = { id } RETURN n with params {id=PK1}
[main] INFO org.neo4j.ogm.drivers.bolt.request.BoltRequest - Request: MATCH (n) WHERE n.primaryKey = { id } RETURN n with params {id=PK1}
null

很遗憾,这是 OGM 中的错误。您可以在这里提出问题:https://github.com/neo4j/neo4j-ogm/issues

作为解决方法,您可以在 Equity class 中声明您的索引,因为 OGM 仅扫描主索引层次结构中的叶 classes。