挣扎着使用主索引从Neo4j中检索实体

时间:2017-03-05 15:30:51

标签: neo4j neo4j-ogm

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

    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;     
    }
}

当我运行App时,Equity会在数据库中按预期创建,所有属性和标记都表示继承人的预期。问题是findPK方法只返回n​​ull。所以从干净的db开始我

  • 找它并且找不到它
  • 创建确定
  • 再找一遍,找不到问题

Logging显示约束的创建,公平的创建以及查找它的尝试。从表面上看,一切都很好,但没有找到任何东西!我非常感谢任何关于此的提示我确定我错过了一些愚蠢的事情。

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

1 个答案:

答案 0 :(得分:1)

不幸的是,这是OGM中的一个错误。您可以在此处提出问题:https://github.com/neo4j/neo4j-ogm/issues

作为一种变通方法,您可以在Equity类中声明索引,因为OGM仅扫描主索引的层次结构中的叶类。