Use @NodeEntity for interface / abstract class
You can definitely do it on Abstract classes
, and I find it good practice in some common cases. Let me give you an example that I am using in my chart model:
@NodeEntity
public abstract class BasicNodeEntity implements Serializable {
@GraphId
private Long nodeId;
public Long getNodeId() {
return nodeId;
}
@Override
public abstract boolean equals(Object o);
@Override
public abstract int hashCode();
}
public abstract class IdentifiableEntity extends BasicNodeEntity {
@Indexed(unique = true)
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof IdentifiableEntity)) return false;
IdentifiableEntity entity = (IdentifiableEntity) o;
if (id != null ? !id.equals(entity.id) : entity.id != null) return false;
return true;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
An example of an object identifier.
public class User extends IdentifiableEntity {
private String firstName;
// ...
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
OTOH, as far as I know, if you annotate an interface with @NodeEntity
, those classes that implement the interface DO NOT inherit the annotation. Of course I made a test to test it and it definitely spring-data-neo4j
throws an exception because it doesn't recognize the inherited class NodeEntity
either RelationshipEntity
.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jMappingContext' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Invocation of init method failed; nested exception is org.springframework.data.neo4j.mapping.InvalidEntityTypeException: Type class com.xxx.yyy.rest.user.domain.User is neither a @NodeEntity nor a @RelationshipEntity
Hope it helps
source to share