Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The name of a RelationshipEntity for a single node relation is limited the relationship name #1132

Closed
oxisto opened this issue Aug 7, 2024 · 7 comments · Fixed by #1134
Closed

Comments

@oxisto
Copy link
Contributor

oxisto commented Aug 7, 2024

This is a little bit of a complicated issue, but it seems I encounter a somewhat weird behaviour, if one uses RelationshipEntity objects for single-node relationships.

Consider the following (Kotlin) code:

@RelationshipEntity
class AstEdge(@StartNode var start: Node, @EndNode var end: Node) {
    @field:Id @field:GeneratedValue private val id: Long? = null
}

@NodeEntity
class FunctionDeclaration : Node {
  @Relationship("BODY")
  var body = AstEdge()
}

(the real example code is a bit more complex with generics, but the issue still seems to persist with this small example)

When trying to persist this I get the following error:

java.lang.NullPointerException: Cannot invoke "org.neo4j.ogm.metadata.ClassInfo.isAbstract()" because "declaredObjectInfo" is null
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:428)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:282)
	at org.neo4j.ogm.context.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:847)
	at org.neo4j.ogm.context.EntityGraphMapper.link(EntityGraphMapper.java:532)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:453)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:282)
	at org.neo4j.ogm.context.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:847)
	at org.neo4j.ogm.context.EntityGraphMapper.link(EntityGraphMapper.java:532)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:453)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:282)
	at org.neo4j.ogm.context.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:847)
	at org.neo4j.ogm.context.EntityGraphMapper.link(EntityGraphMapper.java:532)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:453)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:282)
	at org.neo4j.ogm.context.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:847)
	at org.neo4j.ogm.context.EntityGraphMapper.link(EntityGraphMapper.java:532)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:453)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:282)
	at org.neo4j.ogm.context.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:847)
	at org.neo4j.ogm.context.EntityGraphMapper.link(EntityGraphMapper.java:532)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:453)
	at org.neo4j.ogm.context.EntityGraphMapper.mapEntity(EntityGraphMapper.java:282)
	at org.neo4j.ogm.context.EntityGraphMapper.map(EntityGraphMapper.java:170)
	at org.neo4j.ogm.session.delegates.SaveDelegate.lambda$save$1(SaveDelegate.java:89)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:89)
	at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:495)

If I change the @Relationship name to ASTEDGE (matching the relationship entity class in uppercase), the graph persists without problems. Persisting a list of such entities with BODY also works.

Expected Behavior

I would expect that the above sample works.

Current Behavior

See stack-trace

@meistermeier
Copy link
Collaborator

This is indeed interesting if this should really happen. From what I can see in our test suite this should not be a problem.
Could you provide also the information what Node is in your example?
It would be also helpful to see the test code that sets up the entities and persist them, to get the full picture.

@oxisto
Copy link
Contributor Author

oxisto commented Aug 8, 2024

This is indeed interesting if this should really happen. From what I can see in our test suite this should not be a problem. Could you provide also the information what Node is in your example? It would be also helpful to see the test code that sets up the entities and persist them, to get the full picture.

Hi! Node ist just a normal base class. I will try to distill a running example from our code base during the day today. The code base is open-source, but it might be too complex to just simply run.

@meistermeier
Copy link
Collaborator

This would be very helpful, thanks.

@oxisto
Copy link
Contributor Author

oxisto commented Aug 8, 2024

This would be very helpful, thanks.

I managed to put it together here: https://github.com/oxisto/kotlin-ogm-test
Note that this example is a bit more complex (and contains some generics), but it also fails without the generics.

oxisto added a commit to oxisto/neo4j-ogm that referenced this issue Aug 8, 2024
Seems to be an obvious fix, not sure if this is really enough.

Fixes neo4j#1132
@meistermeier
Copy link
Collaborator

From the given example (and sorry that I missed this in the examples above), I can see that this is expected behaviour in Neo4j-OGM. It fails with the NPE because it cannot find the matching definition for the "unknown" relationship type.
Without a type / value in the @RelationshipEntity annotation it will fall back to UPPER_SNAKECASE for the naming.
The types need to be in sync.
While writing and confirming this in code, I figured out that this is partial true: It is just plain upper case. Don't know why this changed. (I am happy to create a new issue for this)

Also see this in the reference documentation: https://neo4j.com/docs/ogm-manual/current/reference/#reference:annotating-entities:relationship-entity

@oxisto
Copy link
Contributor Author

oxisto commented Aug 9, 2024

From the given example (and sorry that I missed this in the examples above), I can see that this is expected behaviour in Neo4j-OGM. It fails with the NPE because it cannot find the matching definition for the "unknown" relationship type. Without a type / value in the @RelationshipEntity annotation it will fall back to UPPER_SNAKECASE for the naming. The types need to be in sync. While writing and confirming this in code, I figured out that this is partial true: It is just plain upper case. Don't know why this changed. (I am happy to create a new issue for this)

Also see this in the reference documentation: https://neo4j.com/docs/ogm-manual/current/reference/#reference:annotating-entities:relationship-entity

That only seems to be partially true because the relationshipType variable is correctly set to BODY if I debug it. Yes you are correct that it fails with an NPE because it cannot find a relationship entity class for this using the metadata/classdata lookup. However, the mapper seems to be "intelligent" enough to see that AstEdge is in fact a relationship entity and if you add an additional null check, things continue normally (see my PR). It seems the only reason the added look-up is here, is to check whether the returned class (which happens to be null) is abstract. Well if its null its never abstract so the normal control flow could continue.

@meistermeier
Copy link
Collaborator

meistermeier commented Aug 9, 2024

is to check whether the returned class (which happens to be null) is abstract

Unfortunately being null as in "I don't know this entity" differs to plain false. What if the class is abstract but not correctly mapped?
Additionally, there are other places in the code that are trying to resolve the information of a particular class via Metadata#classInfo and are relying on those information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants