带有Quarkus的Neo4J OGM(视频)-Sebastian Daschner


该文演示了一个使用Neo4J数据库和Neo4J OGM的Quarkus应用程序示例,代码:GitHub
在当前版本中1.4.2.Final,Quarkus带有基本的Neo4J支持,但是截至撰写本文时,还没有包括对OGM映射的支持。但是,我们可以使用一个简单的生产者来添加支持,该生产者公开了Neo4J OGM SessionFactory:

import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.SessionFactory;
...

@ApplicationScoped
public class SessionFactoryProducer {

    public static final String PACKAGE = "com.sebastian_daschner.coffee.entity";

    @ConfigProperty(name =
"quarkus.neo4j.uri")
    String databaseUri;

    @ConfigProperty(name =
"quarkus.neo4j.authentication.username")
    String username;

    @ConfigProperty(name =
"quarkus.neo4j.authentication.password")
    String password;

    @Produces
    SessionFactory produceSessionFactory() {
        Configuration neoConfig = new Configuration.Builder()
                .uri(databaseUri)
                .credentials(username, password)
                .useNativeTypes()
                .build();

        return new SessionFactory(neoConfig, PACKAGE);
    }

    void disposeSessionFactory(@Disposes SessionFactory sessionFactory) {
        sessionFactory.close();
    }
}

现在我们可以将注入到SessionFactorybean中,并使用它来查询图数据库:

import org.neo4j.ogm.session.*;
...

@ApplicationScoped
public class CoffeeBeans {

    @Inject
    SessionFactory sessionFactory;

    public List<CoffeeBean> getCoffeeBeans() {
        Session session = sessionFactory.openSession();
        return new ArrayList<>(session.loadAll(CoffeeBean.class,
                new SortOrder("name"), 1));
    }

    public List<CoffeeBean> getCoffeeBeansSpecificFlavor(String flavor) {
        Session session = sessionFactory.openSession();

        Iterable<CoffeeBean> result = session.query(CoffeeBean.class,
               
"MATCH (b:CoffeeBean)-[:TASTES]->(:Flavor {description: $flavor})\n" +
               
"MATCH (b)-[isFrom:IS_FROM]->(country)\n" +
               
"MATCH (b)-[tastes:TASTES]->(flavor)\n" +
               
"RETURN b, collect(isFrom), collect(country)," +
               
" collect(tastes), collect(flavor)\n" +
               
"ORDER by b.name;",
            Map.of(
"flavor", flavor));

        return resultList(result);
    }

    ...
}

CoffeeBean对象是通过Neo4J OGM映射的:
import org.neo4j.ogm.annotation.*;
...

@NodeEntity
public class CoffeeBean {

    @Id
    public String name;

    @Relationship("IS_FROM")
    public Set<Origin> origins = new HashSet<>();

    @Property
    public Roast roast;

    @Relationship(
"TASTES")
    public Set<FlavorProfile> flavorProfiles = new HashSet<>();

    ...
}

点击标题见原文视频