【问题标题】:Spring Data Neo4J (SDN) Saving an entity using Neo4jTemplateSpring Data Neo4J (SDN) 使用 Neo4jTemplate 保存实体
【发布时间】:2017-02-01 03:26:24
【问题描述】:

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

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

我的代码(独立应用):

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

}

我的实体(存在于包 domain 中)如下所示:

@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 时,没有节点存在。

【问题讨论】:

    标签: neo4j spring-data-neo4j spring-data-neo4j-4 neo4j-ogm


    【解决方案1】:

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

    这里解释: Spring Neo4j not save data

    深刻的教训:永远不要手动设置@GraphId。

    【讨论】:

    • 这是一个容易掉入的陷阱,正如您所说,这是一个艰难的教训。虽然不是每个人都同意这种方法,但我经常提供一个 setId() ,它需要一个原始的 long 并检查 id 属性是否已经设置。如果 id 为 null,我确实设置了值,但是,如果 id 不为 null,我只记录一个警告,说明 id 已设置并且此 setter 未采取任何操作。当您想要使用显式设置 id 的实体(为方便起见)但防止在实际使用中产生任何负面影响时,这可以轻松进行单元测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多