Farjam Ramezan pour
Farjam Ramezan pour

Reputation: 15

Neo4j SDN - OGM nodes equality

I have a problem with Spring Data Neo4j and OGM. When I create a node for first time, it`s ok but if I refresh that page I get this error:

Cypher execution failed with code 'Neo.ClientError.Schema.ConstraintValidationFailed': Node(126) already exists with label Country and property name = 'Country-1'.

I searched the web and read so many documents about equals and hashCode but none of them is helping. Here are my classes:

public abstract class Place {

    @Id
    @GeneratedValue(strategy = UuidStrategy.class)
    private String id ;

    private String name ;

    public String getId(){return id ;}

}

@Getter
@Setter
@NodeEntity(label = "Country")
@CompositeIndex(unique = true , properties = "name")
public class Country extends Place {

    private String label = "Country";

    public Country() {}

    @Override
    public int hashCode() {
        int result  = label == null ? 1 : label.hashCode();
        result = 31 * result + this.getName() == null ? 0 : this.getName().hashCode();
        result = 31 * result + this.getId() == null ? 0 : this.getId().hashCode();
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Country)) {
            return false;
        }
        Country that = (Country) o ;
        return this.getName().equals(that.getName())
                && this.getId().equals(that.getId())
                && this.getLabel().equals(that.getLabel());
    }
}

Repository is default. As I know it`s a problem of equality check but how can I fix this?

Upvotes: 0

Views: 136

Answers (1)

meistermeier
meistermeier

Reputation: 8262

You created a constraint on your Country entity by defining

@CompositeIndex(unique = true , properties = "name")

and probably you also enabled the auto index manager feature in the Neo4j-OGM SessionFactory configuration. This is not related to any implementation of hashCode or equals.

This will also explain the behaviour you are facing: First run succeeds but the very same action repeated failed.

Upvotes: 1

Related Questions