首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Data Neo4J (SDN)使用Neo4jTemplate保存实体

Spring Data Neo4J (SDN)使用Neo4jTemplate保存实体
EN

Stack Overflow用户
提问于 2017-02-01 03:26:24
回答 1查看 255关注 0票数 0

新手到SDN和Neo4j。使用sdn版本: 4.1.6.RELEASE)和neo4j版本: 3.1.0。

我正在尝试一种简单的编程方法,在没有任何存储库支持的情况下使用Neo4jTemplate来持久化对象,而且它似乎不起作用。

我的代码(独立应用程序):

代码语言:javascript
复制
public class Scratchpad {

    public static void main(String[] args) throws Exception {
        Configuration config = new Configuration();
        config.driverConfiguration()
                .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
                .setCredentials("neo4j", "xxxx")
                .setURI("http://localhost:7474");

        System.out.println(config);

        SessionFactory sf = new SessionFactory(config, "domain");

        Session session = sf.openSession();

        final Neo4jTemplate neo4jTemplate = new Neo4jTemplate(session);

        PlatformTransactionManager pt =  new Neo4jTransactionManager(session);
        final TransactionTemplate transactionTemplate = new TransactionTemplate(pt);

        transactionTemplate.execute((TransactionCallback<Object>) transactionStatus -> {
            Person p = new Person("Jim", 1);
            p.worksWith(new Person("Jack", 2));
            p.worksWith(new Person("Jane", 3));
            neo4jTemplate.save(p, 2);
            return p;
        });
    }

}

我的实体(出现在包中)如下所示:

代码语言:javascript
复制
@NodeEntity
public class Person {

    @GraphId
    private Long id;

    private String name;

    private Person() {
        // Empty constructor required as of Neo4j API 2.0.5
    }

    ;

    public Person(String name, long id) {
        this.id = id;
        this.name = name;
    }

    /**
     * Neo4j doesn't REALLY have bi-directional relationships. It just means when querying
     * to ignore the direction of the relationship.
     * https://dzone.com/articles/modelling-data-neo4j
     */
    @Relationship(type = "TEAMMATE", direction = Relationship.UNDIRECTED)
    public Set<Person> teammates;

    public void worksWith(Person person) {
        if (teammates == null) {
            teammates = new HashSet<>();
        }
        teammates.add(person);
    }

    public String toString() {

        return this.name + "'s teammates => "
                + Optional.ofNullable(this.teammates).orElse(
                Collections.emptySet()).stream().map(
                person -> person.getName()).collect(Collectors.toList());
    }

    public String getName() {
        return name;
    }

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

日志中没有错误的迹象。但是,当我使用web控制台查询Neo4J时,不存在节点。

EN

回答 1

Stack Overflow用户

发布于 2017-02-01 08:32:51

在更多的研究中,发现问题在于@GraphId字段永远不应该设置值。

在这里解释:Spring Neo4j not save data

吸取的艰难教训:永远不要手动设置@GraphId .

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41971398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档