在接口/抽象类上使用@NodeEntity [英] Use @NodeEntity on interface/abstract class

查看:1311
本文介绍了在接口/抽象类上使用@NodeEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在接口或表象类或它们的字段上从SpringData Neo4j添加@NodeEntity(甚至是@RelationshipEntity)注释?如果没有,您如何处理这些情况?

Is it possible to add @NodeEntity (or even @RelationshipEntity) annotation from SpringData Neo4j on an interface or abstact class or their fields? If not, how do you manage these situations?

推荐答案

您当然可以在Abstract classes上执行此操作,在某些常见情况下,我认为这是一个好习惯.让我给你一个我在图形模型中使用的例子:

Definitely you can do that on Abstract classes, and I think it's a good practice in some common cases. Let me give you an example that I'm using in my graph 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;
   }
}

可识别实体的示例.

public class User extends IdentifiableEntity {
   private String firstName;
   // ...

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
}

据我所知,

OTOH,如果用@NodeEntity注释接口,则实现该接口的那些类不要继承该注释.为了确保我已经进行了测试以检查它并明确地spring-data-neo4j引发了异常,因为它既不能识别继承的类,也不能识别NodeEntityRelationshipEntity.

OTOH, as far as I know, if you annotate an interface with @NodeEntity, those classes who implement the interface DON'T inherite the annotation. To be sure I've made a test to check it and definately spring-data-neo4j throws an Exception because don't recognize the inherited class neither an NodeEntity nor a 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

希望有帮助

这篇关于在接口/抽象类上使用@NodeEntity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆