jhyot
(Jhyot)
January 14, 2019, 12:04pm
1
I found this statement from February 2018 in a Neo OGM Github issue:
If you want to use custom ids the attribute should have a @GeneratedValue
annotation. Additional keep in mind that the name id
is reserved for OGM internal id handling. Please use another name for the attribute.
opened 06:29PM - 13 Feb 18 UTC
closed 04:45PM - 29 Nov 18 UTC
needs investigation
We have a proprietary unique id on all entities in our application, and we would… like to use it as our primary id instead of the old Long @GraphId. Unfortunately, OGM fails when we have both @Id and @Convert with a converter on a field.
## Expected Behavior
The Id should be recognized for adding and updating purposes.
## Current Behavior
org.neo4j.driver.v1.exceptions.ClientException: Cannot merge node using null property value for id
## Possible Solution
Because of the converter, the primaryIdToNativeId map in MappingContext ends up with copies of the same object with a LabelPrimaryId key using both the original type and the converted type (in this case, it's a String). There should probably be something to make sure only the converted or original type ends up in the MappingContext.
## Steps to Reproduce (for bugs)
Create a wrapper Id object:
```
class PrimaryUuid {
private UUID id;
private PrimaryUuid() {
id = UUID.randomUuid();
}
public PrimaryUuid(String s) {
id = UUID.fromString(s);
}
public static PrimaryUuid getPrimaryUuid() { return new PrimaryUuid(); }
//...hashcode, equals, toString, etc.
}
```
Create a Converter:
```
public class PrimaryUuidConverter implements AttributeConverter<PrimaryUuid, String> {
public String toGraphProperty(PrimaryUuid value) {
return value.toString();
}
public PrimaryUuid toEntityAttribute(String s) {
return new PrimaryUuid(s);
}
}
```
Add both as an @Id to an Entity:
```
public class Entity {
@Id
@Convert(converter = PrimaryUuidConverter.class)
private PrimaryUuid id;
// ...gettters, setters, hashcode, equals, etc.
}
```
Create subclasses with relationships
```
public class A extends Entity {
@Relationship(type = "A_RELATIONSHIP")
private B b;
// ..relationships and properties, getters, setters, etc.
}
public class B extends Entity {
@Relationship(type = "A_RELATIONSHIP", direction = Relationship.INCOMING)
private A a;
// ..relationships and properties, getters, setters, etc.
}
```
Add, load and update objects with neo4j database. Eventually you will get:
`org.neo4j.driver.v1.exceptions.ClientException: Cannot merge node using null property value for id`
## Context
Proprietary Id object with converter that we want to use as @Id so we can remove unnecessary and deprecated @GraphId.
## Your Environment
- OGM Version used: 3.1.0
- Java Version used: 8u162b12
- Neo4J Version used: 3.3.2
- Bolt Driver Version used (if applicable):
- Operating System and Version:
- Link to your project:
Is this still true (or even was it ever true)? Is there any documentation regarding this?
This statement is not true anymore. You can name the variable also to id
and use it as a custom id field.