我遇到了neo4j OGM库的问题,我按照库docs page上的说明实现了一个抽象的父实体,以包含我的实体的所有共享字段和功能。当我继承具体的类并尝试执行session.save
时,我收到此错误消息MappingException: No identity field found for class: models.nodes.User
。然后我尝试将id字段从父类拉到子具体类,并且保存操作成功 - 但是,父类的其他字段没有持久保存到DB中。我的结论是OGM出于某种原因忽略了父域。
这是我的父抽象类:
abstract public class MorpheusEntity {
// Fields
/**
* The ID of the entity
*/
@JsonProperty("id")
@GraphId
public Long id;
/**
* The date of first creation of the entity
*/
@DateString
private Date created;
/**
* The date of the last modification of the entity
*/
@DateString
private Date modified;
// Constructors
public MorpheusEntity() {
this.created = new Date();
this.modified = new Date();
}
// Methods
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || id == null || getClass() != o.getClass()) return false;
MorpheusEntity entity = (MorpheusEntity) o;
if (!id.equals(entity.id)) return false;
return true;
}
@Override
public int hashCode() {
return (id == null) ? -1 : id.hashCode();
}
// Getters / Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
}
这是我具体的儿童班:
@NodeEntity
public class User extends MorpheusEntity {
// Fields
/**
* Facebook ID of the user
*/
private String facebookId;
/**
* First name of the user
*/
private String firstName;
/**
* Last name of the user
*/
private String lastName;
/**
* A URL address of the user avatar image
*/
private String avatarURL;
/**
* A set of friends of the user
*/
@Relationship(type = RelationshipNames.USER_FRIENDS, direction = Relationship.UNDIRECTED)
private Set<User> friends;
/**
* A set of user connected devices
*/
@Relationship(type = RelationshipNames.USER_DEVICES, direction = Relationship.OUTGOING)
private Set<Device> devices;
// Constructors
// Methods
// Getters / Setters
public String getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAvatarURL() {
return avatarURL;
}
public void setAvatarURL(String avatarURL) {
this.avatarURL = avatarURL;
}
public Set<User> getFriends() {
return friends;
}
public void setFriends(Set<User> friends) {
this.friends = friends;
}
public Set<Device> getDevices() {
return devices;
}
public void setDevices(Set<Device> devices) {
this.devices = devices;
}
}
我已经尝试将父类放在映射包中,然后放出。 &lt;&lt; - 编辑:这实际上是问题,父类没有被OGM映射
我错过了一些必需的注释吗?我的父类是否未正确映射? 我正在使用来自Maven的使用SBT构建器的Neo4j OGM version 1.1.4的Play Framework 2.4。
感谢。
答案 0 :(得分:1)
创建SessionFactory时,请检查您的抽象类包是否正在注册。
答案 1 :(得分:0)
@NodeEntity
应该在Parent类中,它将被继承到Child类。
请参阅文档中的Entity Type Representation部分