I am working on a Spring Boot application using Neo4j as the database. I have a UserEntity
class that has a relationship with UserContactEntity
. However, when I run a query to fetch UserEntity
along with its contacts
, the contacts
field is always null.
Here are the relevant parts of my code:
UserEntity.kt:
@NodeEntity("User")
class UserEntity : PermissionableEntity, Serializable {
@Id
@Property("userId")
var userId: StringID? = null
@Relationship("PERSON_HAS_CONTACT")
var contacts: MutableSet<UserContactEntity> = mutableSetOf()
// Other fields and methods...
}
UserContactEntity.kt:
@NodeEntity("UserContact")
class UserContactEntity : Serializable {
// Fields and methods...
}
UserRepository.kt:
interface UserRepository : Neo4jRepository<UserEntity, StringID> {
fun findByUserIdIn(userIds: Collection<StringID>, @Depth depth: Int = 2): Iterable<UserEntity>
@Query("MATCH (u:User)-[:PERSON_HAS_CONTACT]->(c:UserContact) WHERE u.userId IN $userIds RETURN u, collect(c)")
fun findByUserIdInWithContacts(@Param("userIds") userIds: Collection<StringID>): Iterable<UserEntity>
}
If I run this query in the database using the Neo4J browser, I can see that contacts are associated correctly with the users.
MATCH (u:User)-[r:PERSON_HAS_CONTACT]->(c:UserContact)
RETURN u.userId AS userId, c.id AS contactId, c.type AS contactType, c.detail AS contactDetail, c.verified AS contactVerified
I have tried both of the following methods to solve the problem:
- Using the
findByUserIdIn
method, which takes a collection ofStringID
and an optional depth parameter to retrieveUserEntity
objects. - Using the
findByUserIdInWithContacts
method, which is a custom query that matchesUser
nodes withUserContact
nodes and returns the user along with their contacts.
Despite the query and the relationship definition, the contacts
field in UserEntity
is always null. What could be causing this issue, and how can I ensure that the contacts
field is correctly populated?
Are there any known issues with Spring Data Neo4j or Neo4j, or could a bug somewhere else in my application be causing this issue?
Versions:
- Neo4j 4.1.1 enterprise
- Spring Boot 2.3.8.RELEASE
- Java 11