filter criteria in the repository interface query method?

107 views
Skip to first unread message

Daniel Aschauer

unread,
Apr 30, 2012, 8:47:01 AM4/30/12
to ne...@googlegroups.com
Hi!
I am wondering if there is any possibility to build an interface query in the repository like this, where I can pass additional filter criteria by string argument

    @Query("start item=node:ProductItem(tpg = \"{0}\") where {1} return item")
    Iterable<ProductItem> findByTpgFiltered(String tgp, String filter);

when giving additional filters like findByTpgFiltered("XYZ", "item.name <> \"itemA\"");
I get an error.
What would be a better way to do that?
I use spring data neo4j 2.0.1 and neo4j 1.6

Thanks,
Daniel


Michael Hunger

unread,
Apr 30, 2012, 9:48:49 AM4/30/12
to ne...@googlegroups.com
Unfortunately not, as cypher does not allow structural parameters.

But you could use cypher-dsl to create the query and then use the CypherDSLRepository, those type-safe query objects can be extended later with additional filters etc.


HTH

Cheers

Michael

Tero Paananen

unread,
Apr 30, 2012, 10:19:12 AM4/30/12
to ne...@googlegroups.com
> I am wondering if there is any possibility to build an interface query in
> the repository like this, where I can pass additional filter criteria by
> string argument
>
>     @Query("start item=node:ProductItem(tpg = \"{0}\") where {1} return
> item")
>     Iterable<ProductItem> findByTpgFiltered(String tgp, String filter);
>
> when giving additional filters like findByTpgFiltered("XYZ", "item.name <>
> \"itemA\"");
> I get an error.
> What would be a better way to do that?

Since your query is so simple, I think the easiest way would be to build
they query string at runtime and execute it using Neo4jTemplate.

String query = String.format("start item = node:ProductItem(tpg =
\"%s\") where %s return item", tpg, filter);
Result<Map<String, Object>> result = template.query(query, new
HashMap<String, Object>());
return result.to(ProductItem.class);

There should be examples of how to use Neo4jTemplate with SDN
on the mailing list or the SDN docs. You should be able to inject
the Neo4jTemplate into whatever classes you have accessing
Neo4j via SDN.

-TPP

Daniel Aschauer

unread,
May 2, 2012, 10:16:57 AM5/2/12
to ne...@googlegroups.com
Thanks for your advise!
@Michael:
I don't think the use cypher DSL queries CypherDSLRepository is appropriate, as I will apply additional filters dynamically.
I resolved it as described by Tero right now, works fine!

Michael Hunger

unread,
May 2, 2012, 10:51:18 AM5/2/12
to ne...@googlegroups.com, rickar...@neotechnology.com
But you can do exactly that with cypherdsl build a base query that is stored in a var

And the add the filters, sorts and limits on demand.

Michael

Sent from mobile device
Reply all
Reply to author
Forward
0 new messages