The Best Way To Use The Dataset For Application. » My Blogging

The best way to use the dataset for application.

Let’s move to a small project I’ve done with my colleague from DevRel, Jennifer Reif, and the supervision of Gerrit Meier, who is co-maintainer of Spring Data Neo4j 5, 6, and OGM as well. So it’s not the same project as before, it’s using the “movies” dataset. So, you know if you go to Neo4j Desktop, you can do something like- Maybe not on that database, but- I don’t know. I’m just going to use this one. Or maybe “Disposable”, that’s even greater. You can do something like play movies in the prompt, and it will import the data.


Let me just show you, just so you can try it at home as well. It’s starting. It’s based on this “movies” dataset, where you have actors, producers, movies, etc. So you probably have seen that, because many documentation sections in Neo4j are using that dataset, ’cause it’s quite simple to understand, ’cause movies are usually a familiar concept to people. So let me just go- It’s disposable, I can just truncate everything. Get rid of stuff. Of course, I forgot to write “DETACH”. Oh no, I wrote only “DETACH” and forgot to write “DELETE”.

You can say something like, “play movies” to play the movie guide. Then you can learn about Cypher and the “movie” dataset. You can run those and get a small movie dataset, etc. Of course, you can use Neo4j Sandbox to get a ready-made dataset as well, for an online instance of Neo4j, but you can also do it locally with Neo4j Desktop. And then you’ve got slides you can go through, with some queries, etc.

I’m not going to do a tutorial, but yeah. There are a few datasets like that, you can explore with Neo4j Desktop. It’s really great onboarding material. So basically what we have right now, is a classic Spring Data Neo4j 5/OGM application. So we have, here we just have the data layer, we don’t have any web stuff, no API over it, we just have entities. So we have an Actor that has a “name”. We have a “Movie”. So that’s an entity, actually. So the entity you’ve mapped onto a “Movie” label. We have a Tagline, so we have a custom type for that. We have the release dates, and we’re using optimistic locking as well.

“Tagline” is a custom type that is not supported by default, so you need to use a converter, which is this “AttributeConverter” in Spring Data Neo4j 5. I believe it’s Spring Data- Oh, no. That’s a Neo4j-OGM thing as well, right? You’ve got the conversion as well. And so you’ve got movies, you’ve got Taglines, etc. Then you’ve got Persons with “Projection”, so that’s something that allows you to map arbitrary data to some data types that are not entities. In that case, we have “name” and “born” dates, birth date.

We have a Person which is much richer, it is a node entity, which has an internal node ID, the “name”, birth date, and then a bunch of relationships, what movies it directed, what movies it acted in, what movies it reviewed, ’cause you also have reviews like comments, notes, etc., on movies. “produced movies”, it could be any of that. And we have “Relationship” as well. So RelationshipEntity, which is reviewed between a Person and a movie. And yeah, so you get to this kind of data model here. It’s using Neo4j repositories. That concept still exists in Spring Data Neo4j 6, it still exists there.

It exposes everything like “save()”, “read()”, etc., page-to-page stuff. And we have some custom queries based on the naming convention of Spring Data, so that’s something that is still supported today. We have custom queries, and I will come back to that in a minute, ’cause those we’ll need to change a little. And yeah, that’s about it really. And so if we look at the test part, it’s not too much different from what I’ve done. So Test containers work very fine with JUnit 4, JUnit 5, Spring Data Neo4j 5, Spring Data Neo4j 6. And so in that case, we are doing exactly the same as I was doing in my application, right? That part remains unchanged. And so make sure to use the latest OGM.

That’s true anywhere, by the way. If you should want to migrate to Spring Data Neo4j 6, first make sure to migrate to the latest Spring Data Neo4j 5-OGM. And so you’ve got that for free. So that’s supported by recent-ish versions of Spring frameworks. So that’s something that’s not new. We actually test. We actually import some of these datasets, so it’s equivalent to playing in a way. And we’ve got several tests, just testing the basic stuff we need. And so, as I was telling you, we will- There are a few things to be aware of if you want to migrate to Spring Data Neo4j 6. The first thing is, “@RelationshipEntity” is not a thing anymore in Spring Data Neo4j 6, so that will have to be transformed to “@RelationshipProperties”.

They’re not going to be If you have a repository for that, you will have to migrate it. And for that, you can use Neo4jTemplate, or you can use Neo4j Client, or even the Role driver if needed. So the REST looks kind of similar. The nodes haven’t changed much. The annotation names have changed, but the annotations are quite similar to what you had before. And so if we look now, if I switch, to Spring Data Neo4j 6, we will see a few changes. I think one of the most tricky things is understanding the custom query stuff. That’s something that people usually have issues with when they start using Spring Data Neo4j 6. So if I come back to those two. I’m just going to copy it somewhere. So “File/New/Scratch file”.

Yeah. I’m just going to have it here. We’re trying to find a Projection, so it’s annotated with “@QueryResult”. We can use “born” and “name”, so we match a Movie per its title, and we’ll try to find Persons that are active in that specific movie, and then we return the name of that person and the birth date of that person. And we project that into this “Projection” class with “name” and “born”, so the property names match directly. And similarly, for the other one, we basically try to find all the movies in which Tom Hanks has played. So if we look at the test for those two, you can see we have exactly this. I’m just going to put it there.

For the first one, we have this test. For the second one, we have “Tom Hanks”. We have this test. The test here looks weird, but I will tell you what it’s trying to show, actually. So you can see that we return- If you think in terms of pure Cypher if we look at the query again. If I run this, again my Neo4j database, with the “movies” datasets, I should get as many “Tom Hanks” as movies he played in. Right? So they can go there. Let me use the browser. Let me run this query and we’ll see what happens. The visualization gives you this.

In this, there is only one node in the graph, so we visualize only one “Tom Hanks” node. However, if you think in terms of results, that’s not actually quite true. The best way to see that is to use the table view. In the table view, you will see that “Tom Hanks” is repeated once per row, which makes sense, as many movies as Tom Hanks acted in. We have as many rows as movies, so Tom Hanks is repeated all over the place. But you can see that Neo4j-OGM, it tries to be smart and says, “Hey, you know what? This node, I already have it. So it should be returned only once.

Related Posts

Leave a Reply

Your email address will not be published.