What is the best way to look into generated query in Spring Data Neo4j?

Let’s look at the generated query that caused these problems. Oh, yeah. It’s generated different variables every time. Okay. That’s interesting. That is interesting. It does not make- So I will have to chat- Maybe that’s something I missed. Or maybe it’s documented. Let’s see. Because that’s a possible trap that one can fall into. And let’s see an example. “(Movie_.MOVIE.TITLE.getname()).ascending()” Yeah. What about something a bit bigger? “PERSON.BORN”, “PERSON.BORN”.

I know the issue. So let me go back to what I had. The only problem here is I should use “_Connector”? I keep putting the underscore in the wrong place. That’s what I should do! And then I should do this. That way, I- ‘Cause yes, they are nodes in the Cypher DSL sense, but also, I’d rather use a concrete type, so I can have access to the concrete properties of those. In that case, there is no “VALUE”, as we would like, so that’s something to investigate, but now it should work better, ’cause “connector” still named “c”, “transport” is still named “t”. And I don’t lose the type information actually, so that’s great. That’s generics done right, so thank you, dear colleagues.

If I run this again, hopefully, this time the test will pass, ’cause I’m using a stable name for the variable, and I’m reusing that all over the query, so that should be good enough. So that’s why I wanted to take one hour and a half because we never know if I don’t have unexpected stuff that happens. I planned to use 30 minutes, and it’s already thirty-five past, so I did well to take some wiggle room. And now it works! So yeah, small “gotcha”. Of course, you have to use the concrete types if you want to continue accessing the concrete properties, that makes sense but you know, sometimes I make silly mistakes. Cool.

Now let’s switch gears completely. And I’m gonna talk about a related topic, but quite unrelated to the application I’m doing. ‘Cause I didn’t duplicate my project to write it in SDN 5/OGM. I didn’t go that far. But I have another project I want to show you, and I want to highlight some of the important aspects of migrating from Spring Data Neo4j 5/OGM to Spring Data Neo4j 6. So let me first sit down ’cause I’ve been standing for an hour straight. I’d rather sit down a bit now. And then let me change the title, ’cause it’s not Cypher DSL anymore, it’s, it’s about migration. So, “From SDN 5/OGM to SDN 6”. Right.

Yay! Here we go! So let’s remove this. Now we are done with our meta-model as there is a tiny issue, but I will open- I will first investigate and then open an issue if needed. So thank you, Cypher DSL, it was great meeting you so early- And you don’t have to use it with Spring Data Neo4j by the way, if you don’t use it, so if you don’t need SDN, you can still use Cypher DSL, it’s great. There are even some examples for Cypher DSL, so that’s great. And let’s move to Spring Data 5/Neo4j 6 migration. And to set things a bit clear, let me show you what I mean when I talk about this.

So, what’s Spring Data, and what’s Neo4j? Or, what’s Spring Data Neo4j 5, and what’s OGM? So let’s start maybe with Neo4j-OGM. I’m going to Google it, as everyone does. So, Neo4j-OGM is a library. It’s a library. It’s a standard library. There is no adherence to Spring. It works well with Spring projects, but you don’t use Spring frameworks.
It works well with Java Enterprise Edition products, so, yeah. That’s a standalone library, exposing object graph mapping capabilities, like the one we’ve seen with Spring Data Neo4j 6. It can have node entities, relationship entities, and you also have standalone things like- Similar to Hibernate, if you know Hibernate a little, like “SessionFactory”, “Sessions” etc. And so I can give you a quick, quick example, a quick live-coding example of OGM.

It’s going to be very basic, but just to give you a taste of what it can be. And then I will talk about triangulation.
Let’s spend the next 10, 15 minutes just talking about OGM, and see how we can get started with it. Also, again, you should not start with this. If you start a new product, use Spring Data Neo4j 6 directly, unless you have specific needs, and I will come back to that. So one of the things you need to understand, is Spring Data Neo4j 5 was built upon Neo4j-OGM. So it was delegating a lot of mapping logic to Neo4j-OGM. And Spring Data Neo4j 5 and OGM were included by default, when you went to “start.spring.io” for instance, which is “spring initializer”.

When you wanted to add Neo4j, so you would write “Neo4j”, then you would add your metadata, generate, download the project, and get started, you would get Spring Data Neo4j 5-OGM by default, up until Spring Boot 2.3. So like, if you pick “2.3.9” currently, which is the latest release of Spring Boot 2.3, you would still get Spring Data Neo4j 5. If you want- If you pick the latest Spring Boot, which is currently 2.4, then you will get Spring Data Neo4j 6. And just to make things extra explicit, Spring Data Neo4j 6 does not use Neo4j-OGM at all. So Neo4j-OGM is a standalone object graph mapping library, that has been used by Spring Data Neo4j up until version 5. Version 6, which is the current major, does not use it at all.

You get Spring Data Neo4j 5 and OGM up until Spring Boot 2.3 by default. And that is on by default. If you pick the current Spring Boot, which is 2.4, or if you’re feeling adventurous, 2.5, which is currently a milestone, you would get Spring Data Neo4j 6. And there are no dependencies from Spring Data Neo4j 6 the OGM. There are ways to use Spring Data Neo4j 5 and OGM with the latest Spring Boot but again, that’s not recommended, so I’m not even going to show it.

You can try if you really, really need to, but just should first and foremost try to migrate to Spring Data Neo4j 6, and that’s what I’m going to cover next. Because I’m a bit lazy, you can actually use a trial script, which does the same as clicking on 2.3.9, add in some information, in that case, I just used the default metadata. And so what this script does- Maybe move this here. I’m just going to download the default, which is Spring Data Neo4j 5 because I’m using 2.3.9 for Spring Boot.

I’m just going to run this. So here I have a project. Happy. And I’m gonna start just showing you, not the Spring Data Neo4j part, but mostly the Neo4j-OGM part, which is included in the classpath, so that’s the lazy way to Neo4j-OGM. So let me do this. I need to open the right project. So again, for some reason, IntelliJ doesn’t want me to use “Command + V”. I have to do it like this. So if I go there, I’m going to import the project in IntelliJ, and I’m just going to show you a very basic example of Neo4j-OGM.

I’m not going to use Spring Data Neo4j 5 capabilities. So, the way it works- So let’s forget even about Spring Boot, ’cause again, I’m not even interested in Spring Boot here, or any Spring stuff. Just Spring Data Neo4j. So what I can do, is- Sorry, switching between windows, but I just want to check something real quick.  And I currently have Neo4j Desktop open, with one project and one database, which is Neo4j Admin.

Leave a Comment