All Those Beautiful Records…


Java 17 Records and the Neo4j-Java-Driver

Back in March 2020 Java 14 has been released with the first preview of java.lang.Record respectively the record keyword: https://docs.oracle.com/en/java/javase/17/language/records.html. Record classes, which are a special kind of class, help to model plain data aggregates with less ceremony than normal classes and can be thought of as nominal tuples. Records did receive a lot functionality since then and will continue to do so.

Photo by Mick Haupt on Unsplash

However, “Record” is such a nice synonym for Tuple that it has been used extensively elsewhere, especially in database connectivity. Abstractions for relational databases like jOOQ deal in records and so do the Neo4j-Drivers, especially the Java-Driver. For a long time now we have org.neo4j.driver.Record which represent a tuple or a row if you so will in the greater result set of a query.

This has not been a problem until Java 14. With Java 14 however, java.lang.Record appeared and as it is in the java.lang package, it is always imported into your Java program, even though the chances that you will use the class that backs the actual records, are close to zero. Most IDEs, prominently IntelliJ, won’t suggest any import for you on JDK 17 these days for a type of Record because it’s there already. Also, star-imports such as org.neo4j.driver.* , will lead to an error during compilation stating java: reference to Record is ambiguous, both interface org.neo4j.driver.Record in org.neo4j.driver and class java.lang.Record in java.lang match.

As a user of the Neo4j-Java-Driver that can lead to awkward situations in which you will want to process a result set but can’t access its items, as the “wrong” record type is imported into your scope or you just end up with above compilation error.

That didn’t happen to often in the past, but with the brand new Neo4j-Java-Driver 5.0 and higher we bumped our requirements to the latest Java LTS release which is version 17 and here this will happen. This is not a bug in the Java driver or in your code, it’s just something we as a provider of that library need to communicate, and we are not alone in this regard and others also chose to communicate things instead of changing their record implementation.

Here’s a working example that should explain things:

Also, please don’t use star- or wildcard-imports, really.

Happy coding!


All Those Beautiful Records… was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.