Cannot persist anything using SpringData Neo4j

I'm trying to convert an existing springboot backend that seeds a database with a bunch of relationships. I don't know where the issue is, but a driver instance is created, then it gets shut down with nothing persisted. Since i'm not sure where the problem is, here's the entire project. Hopefully somebody else knows what I have to do.

One thing I know is not happening is the saves. There are many like this:

animalRepository.save(americanAlligator);
animalRepository.save(americanBison);
animalRepository.save(asianElephant);
animalRepository.save(asianOtter);
animalRepository.save(baldEagle);
animalRepository.save(bobcat);
animalRepository.save(carpetPython);
animalRepository.save(californiaSeaLion);
animalRepository.save(giantPanda);
animalRepository.save(manedWolf);
animalRepository.save(orangutan);

I have relationships between everything that's supposed to be persisted, but I'm not sure what the deal is with trying actually do it. The entire springboot app will just quit and I get no useful message stating why. Relationships look like this:

@Relationship(type = "FEEDS")
private Set animals = new HashSet<>();

@Relationship(type = "FEEDS", direction = Relationship.INCOMING)
private Set employees = new HashSet<>();

The first would be in a bean named Employee as in a zookeeper, the second would be an animal like a Red Panda. This should be simple enough and it never gets more complicated than that.

Do you know if you were able to curl the endpoints you set up in the Controller? Even though there is no data in the database, you should at least get an empty result set back.....

It looks like the project that your version is forked from is pretty complex. I might start with a single piece, and then build up each component once you ensure the first bit works. For instance, start with the Animal class, Animal repository, and Animal controller. Once you have those 3, you should be able to curl your end point. I've included a very basic example of using Spring with Neo4j from my Github to give you an idea of the dependencies and structure needed. We have other examples I can send along, as well, if needed! :slight_smile:

Cheers,
Jennifer

The backend works, as the master branch of that repo is the same code, but postgres instead of Neo4j. The problem isn't any of the controllers, it's something with Neo4j. If you can wait ten seconds for heroku to wake it up, the hosted version will return results as it's supposed to.

As for your example, I noticed it was using GraphRepository instead of Neo4jRepository, so why is that? Your backend does work, however, everything you used seems to be deprecated as of 2.0.0.RELEASE of spring-boot-starter-parent.

After running it a few times, it looks like nothing gets persisted until you directly define each relationship, which is not how relational databases work. I'm not sure why this is, but save should persist an object regardless of whether or not I point it to anything.

The problem with your project is that you are using the @GeneratedValue id combined with an Integer. The id field has to be a Long in this case.
I have to admit that this is hard to spot because it does not even get mentioned in the log's warnings.

That doesn't explain what I found with Jennifer's project. Why won't hers persist until connections are made. I did, however, verify your finding on my end. Changing everything to Long solved my issue of persistence. I'd like to make it clear the @GeneratedValue does allow Integer for Postgres, which is a feature difference between the two databases that seems to caused this entire problem. As you already noticed, there was no indication that this was even a problem via logging and that should probably be rectified in future releases.

Author's note: this is not meant to be a smart zoo. Alligators should never be unattended.

Sorry, that's my fault. I need to update that example. I obviously didn't thoroughly look through the code before I sent it (just skimmed for the structure).

When I first created that project, I was following an older example. Looks like they have deprecated several pieces of the functionality, which is what caused the lack of persistence. The @GraphRepository is older notation, and the Neo4jRepository has been added as a parent of GraphRepository (you can see that here: https://docs.spring.io/spring-data/neo4j/docs/4.2.5.RELEASE/api/org/springframework/data/neo4j/repository/GraphRepository.html). I believe most of our examples use the Neo4jRepository superinterface.

I apologize for the confusion, and I'll get my basic example updated!

Cheers,
Jennifer

It's fine. The old project is still runnable, just not with springboot 2.0.0. I think I may have also been using the wrong query when stepping through the project, so some objects may have been there that just weren't showing up.

1 Like