cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4j-OGM session.query returns invalid nodes

sim-jac
Node Link

Hy,

we have a node (Article) which has a relationship (previousArticle) to itself. In a cypher query we want to query the databse for all nodes with a specific typeNumber (which is unique). So the quey should return only one node.

If we create two nodes with different typeNumbers the query below returns correctly one node for each typeNumber.

But if we add the one article as previousArticle to the other the query returns both nodes which is absolutely wrong because one of the nodes have a wrong typeNumber .

The first_marker.png debug screenshot from QueryTest:13 shows correclty onle one resulting article with typeNumber "1", although two articles exists in the database (One with "1" and the other with "1x"). The second_marker_1.png and second_marker_2.png from QueryTest:23 however shows both articles in the result set. Clearly visible is the wrong typeNumber at the second entry (Article@24536).

Any advice what I am missing here are highly appreciate 🙂

Thanks, best regards and happy christmas

Simon

 

 

@NodeEntity(Article.NODE_LABEL)
public class Article {
    public static final String NODE_LABEL                       = "Article";
    public static final String TO_PREVIOUS_ARTICLE              = "PREVIOUS_ARTICLE";

    @Property("type_number")
    @Index
    private String typeNumber;

    @Relationship(type = Article.TO_PREVIOUS_ARTICLE)
    private Article previousArticle;
}
public QueryTest {
 @Test
    public void testQuery()
    {
        var previous = Seed.article().build();
        previous.setTypeNumber("1x");
        productService.articleRepo().save(previous);

        var article = Seed.article().build();
        article.setTypeNumber("1");
        productService.articleRepo().save(article);

        var byTypeNumber = findArticleByTypeNumber(
            previous.getTypeNumber()
        );

        Assertions.assertNotNull(byTypeNumber);
        Assertions.assertEquals(byTypeNumber.getId(), previous.getId());

        article.setPreviousArticle(previous);
        productService.articleRepo().save(article);

        byTypeNumber = findArticleByTypeNumber(
            article.getTypeNumber()
        );

        Assertions.assertNotNull(byTypeNumber);
        Assertions.assertNotNull(byTypeNumber.getPreviousArticle());
        Assertions.assertEquals(byTypeNumber.getPreviousArticle().getId(), previous.getId());
    }

    private Article findArticleByTypeNumber(String typeNumber)
    {
        var params = new HashMap<>(Map.of("typeNumber", typeNumber));

        var cypher = "MATCH (a:" + Article.NODE_LABEL + " {type_number : $typeNumber})"

            + " OPTIONAL MATCH (a)" +
            "-[r_a_previous:" + Article.TO_PREVIOUS_ARTICLE + "]" +
            "-(previous_article:" + Article.NODE_LABEL + ")"

            + " RETURN *";
// queryForObject fails with an exception
        var r =
            StreamSupport
                .stream(
                    this.productService.articleRepo()
                        .getSession()
                        .query(
                            Article.class,
                            cypher,
                            params
                        )
                        .spliterator(), false)
                .toList();

        Assertions.assertEquals(1, r.size());
        return r.get(0);
    }
}

 

 

 

 

Neo4j Version:
5.2.0-community

Dependencies:
"org.neo4j" % "neo4j-ogm-core" % "4.0.1",
"org.neo4j" % "neo4j-ogm-bolt-driver" % "4.0.1",
"org.testcontainers" % "neo4j" % "1.17.6" % Test,
"org.testcontainers" % "junit-jupiter" % "1.17.6" % Test,

Java-Version:
openjdk 17 2021-09-14

Framework:
Play-Framework 2.8.16

 

 

1 ACCEPTED SOLUTION
1 REPLY 1