Use @NodeEntity for interface / abstract class

Is it possible to add annotation @NodeEntity

(or even @RelationshipEntity

) from SpringData Neo4j

to an interface or paragraph class or their fields? If not, how do you deal with these situations?

+2


source to share


2 answers


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

+3


source


@NodeEntity or @RelationshipEntity needs to be defined on POJOs or concrete classes. Think of it the same as @Entity in Hibernate. But do you see any valid use case for annotating interfaces or abstract classes?



0


source







All Articles