0

I'm running into a problem with neo4j OGM library, I've followed the instruction on the library docs page to implement an abstract parent entity to contain all the shared fields and functionality of my entities. When I then inherit this class with a concrete one and try to perform session.save, i get this error message MappingException: No identity field found for class: models.nodes.User. I then tried to pull the id field down from the parent class to the child concrete class and the save operation succeeded - but, the other fields of the parent class were not persisted into the DB. My conclusion was that the OGM ignores the parent fields for some reason.

Here is my parent abstract class:

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;
    }

}

And here is my concrete child class:

@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;
    }

}

I've tried to both place the parent class inside the mapped package and out. <<-- EDIT: This was actually the problem, the parent class was not mapped by the OGM

Am I missing some required annotation? Is my parent class not mapped correctly? I'm using Play Framework 2.4 with Neo4j OGM version 1.1.4 from Maven using SBT builder.

Thanks.

Aviv Carmi
  • 907
  • 8
  • 21

2 Answers2

3

Please check that your abstract class package is being registered when the SessionFactory is created.

Vince
  • 2,181
  • 13
  • 16
  • Problem solved! The OGM didn't map the parent class. The section about the [abstract entities in the docs](http://neo4j.com/docs/ogm/java/stable/#__graphid) would be clearer if it would mention that the abstract class package should be referred from the `new SessionFactory` call. I should have tried it myself though as I thought I did. Thanks for the feedback. – Aviv Carmi Jan 24 '16 at 17:24
0

@NodeEntity should be at the Parent class and it will be Inherited to Child class.

See Entity Type Representation section in docs

Sumit
  • 1,400
  • 7
  • 9
  • No it doesn't do it. Tried to add the annotation to the parent class and got the same error, and then I also tried removing it from the child parent and it also gave me the same error. Also, as [the docs](http://neo4j.com/docs/ogm/java/stable/#__graphid) suggest, the parent abstract class seem not to be annotated. – Aviv Carmi Jan 24 '16 at 15:27