Issues while retrieving existing nodes using Spring Data Neo4j - neo4j

I created a simple SDN project to retrieve existing nodes from my database. In the repository, I defined a custom query using #Query annotation which is something like
#Query("MATCH (EMP:EMPLOYEE) WHERE EMP.empName={0} return EMP")
public Employee findByName(String empName);
#RelationshipEntity(type = "HAS_ADDRESS")
class AddressRelationShip
{
#GraphId
Long id;
#StartNode
Employee employee = null;
#EndNode
Address address = null;
public AddressRelationShip(Employee employee, Address address)
{
this.employee = employee;
this.address = address;
}
}
#NodeEntity
#TypeAlias("EMPLOYEE")
public class Employee
{
#GraphId
Long id;
String empName = null;
#RelatedTo(type = "HAS_ADDRESS", direction = Direction.OUTGOING)
#Fetch
Set<Address> addresses;
public void addressEmplployee(Address address)
{
if (addresses == null)
{
addresses = new HashSet<Address>();
}
//AddressRelationShip addressRelationShip = new AddressRelationShip(this, address);
addresses.add(address);
}
public Set<Address> getAddresses()
{
return addresses;
}
public void setAddresses(Set<Address> addresses)
{
this.addresses = addresses;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getEmpName()
{
return empName;
}
public void setEmpName(String empName)
{
this.empName = empName;
}
}
With this query, on execution I get the below error message:
No primary SDN label exists .. (i.e one starting with _)
I googled about the issue and tried to use the below query:
MATCH (EMP:EMPLOYEE:_EMPLOYEE) WHERE EMP.EmployeeId={0} return EMP
This query runs but it doesn't return any response.
One important thing here is that I haven't created the existing nodes using SDN(I googled and found that SDN adds some metadata e.g, _ to nodes/relationships).
However, if I created the (Employee)-[HAS_ADDRESS]->(ADDRESS) pattern data using SDN, below query works fine:
MATCH (EMP:EMPLOYEE) WHERE EMP.empName={0} return EMP
In this case, I found one other issue that it returned address data as well while I'm only returning Employee in the query.
I'm able to obtain the addresses from the Employee entity object.
Any pointers on the above issues?
PS - Neo4j is running in standalone server mode.
Regards,
Rahul

I could resolve the above issues in following steps:
No primary SDN label exists .. (i.e one starting with _) - In SDN 3.3.0, for existing nodes, SDN requires an extra label (in my case, _EMPLOYEE), so a data migration is required. In SDN 4.0, It seems that this is no longer needed but I haven't tried 4.0 yet.
Returning address data as well while I'm only returning Employee in the query - Removing #Fetch on Set addresses in Employee resolved this, However, addresses nodeIds were still returned.
To run SDN 3.x.x with existing data, following data migration is required:
Add additional NodeLabel(preceding the original label with _) to nodes, e.g, add _Employee label to all Employee nodes.
Add __type__ property to nodes and relationships whose value will be fully qualified name for the appropriate domain/model classes, e.g,
match (n:Employee) set n.__type__="org.neo4j.domain.Employee"
Cheers,
Rahul

Related

Neo4j spring - Create a 2 way relationship between same type of entities

Type of relation we want is as follows
CREATE (jack:Person {name:"Jack"})-[:KNOWS]->(jill:Person {name:"Jill"})
WITH jack, jill
CREATE (jack)<-[:KNOWS]-(jill);
How do we model this in the following?
#Node
public class User {
#Id
#GeneratedValue
private UUID id;
private String name;
private String phoneNumber;
private String email;
private boolean isActive;
#JsonIgnore
#Relationship(type = "Knows", direction = Relationship.Direction.OUTGOING)
private Set<Knows> knows;
#JsonIgnore
#Relationship(type = "Knows", direction = Relationship.Direction.INCOMING)
private Set<Knows> knownBy;
public User() {
}
public void knows(Knows to) {
if (knows == null) {
knows = new HashSet<>();
}
knows.add(to);
}
public void knownBy(Knows from) {
if (knownBy == null) {
knownBy = new HashSet<>();
}
knownBy.add(from);
}
}
#RelationshipProperties
public class Knows {
private String as;
#CreatedDate
private Instant createdAt;
#JsonIgnore
#TargetNode
private User User;
}
snippet to save the users
User user1 = userRepository.findById(id1);
User user2 = userRepository.findById(id2);
Knows knows = new Knows("as", Instant.now(), user1);
Knows knownBy = new Knows("as", Instant.now(), user2);
user2.knows(knows);
user1.knownBy(knownBy);
userRepository.save(user2);
With this, we are getting StackOverflow while saving the User entity.
We want to understand is this the correct way to model it or is there a better way?
First things first: There is a bug in SDN when it comes to map bidirectional relationships to the database.
Ticket to track is https://jira.spring.io/browse/DATAGRAPH-1469
It will be very certain in the next release.
Giving you scenario I would suggest cleaning up the relationships and only create the OUTGOING relationships like
#Node
public class User {
// ... id etc.
#Relationship("KNOWS")
private List<Knows> knows;
}
When it comes to setting the value, both sides need to get populated:
user1.knows(user2);
user2.knows(user1);
Given the fact that the bug is present until SDN 6.0.3 gets released, I would suggest to either work either unidirected, only connect the Users in one direction or link the Users directly.
As a consequence you would have no access to the properties on the relationship.
#Node
public class User {
// ... id etc.
#Relationship("KNOWS")
private List<User> knows;
}

How to do group by key on custom logic in cloud data flow

I am trying to achieve the Groupby key based on custom object in cloud data flow pipe line.
public static void main(String[] args) {
Pipeline pipeline = Pipeline.create(PipelineOptionsFactory.create());
List<KV<Student,StudentValues>> studentList = new ArrayList<>();
studentList.add(KV.of(new Student("pawan", 10,"govt"),
new StudentValues("V1", 123,"govt")));
studentList.add(KV.of(new Student("pawan", 13223,"word"),
new StudentValues("V2", 456,"govt")));
PCollection<KV<Student,StudentValues>> pc =
pipeline.apply(Create.of(studentList));
PCollection<KV<Student, Iterable<StudentValues>>> groupedWords =
pc.apply(GroupByKey.<Student,StudentValues>create());
}
I just wanted to groupBy both the PCollection record based on the Student object.
#DefaultCoder(AvroCoder.class)
static class Student /*implements Serializable*/{
public Student(){}
public Student(String n, Integer i, String sc){
name = n;
id = i;
school = sc;
}
public String name;
public Integer id;
public String school;
#Override
public boolean equals(Object obj) {
System.out.println("obj = "+obj);
System.out.println("this = "+this);
Student stObj= (Student)obj;
if (stObj.Name== this.Name){
return true;
} else{
return false;
}
}
}
I have overridden the equals method of my custom class, but each time i am getting same instance of Student object to compare inside equals method.
Ideally it sholud compare first student key with second one.
Whats wrong i am doing here.
Why do you think you are doing anything wrong? The keys of each element are serialized (using the AvroCoder you specified) and the GroupByKey can group all of the elements with the same serialized representation together. After that it doesn't need to compare the students to make sure that the values with the same key have been grouped together.

Spring Neo4j - Bolt driver fails to load a relationship while the Http driver does it successfully

While coding a poc with Spring Neo4j using spring boot I came across what it seems to be an inconsistent behavior between the Bolt driver and the Http driver. Basically after saving a rich relationship between 2 nodes the test fails to load it when using the Bolt driver, however the exact same test succeeds when trying with the Http driver.
The sample project can be downloaded from github
It's a very basic/straight forward test, the only pre-requisite is that you will need to have Neo4j 3 installed with the Bolt connector enabled.
As suggested by Andrej please find below the relevant sections of the code:
#NodeEntity(label = "Person")
public class Person {
private Long id;
private String firstname;
private String lastname;
#Relationship(type = "HAS_CONTACT", direction = Relationship.INCOMING)
private Contact contact;
// getters and setters here .........
}
#NodeEntity(label = "BankAccount")
public class BankAccount {
private Long id;
private Integer balance;
#Relationship(type = "HAS_CONTACT")
private List<Contact> contacts = new ArrayList<>();
// getters and setters here .........
}
#RelationshipEntity(type = "HAS_CONTACT")
public class Contact {
public Contact() {
}
public Contact(BankAccount bankAccount, Person person) {
this.bankAccount = bankAccount;
this.person = person;
this.bankAccount.getContacts().add(this);
this.person.setContact(this);
}
private Long id;
#StartNode
private BankAccount bankAccount;
#EndNode
private Person person;
private String email;
private String phoneNumber;
// getters and setters here .........
}
#Repository
public interface ContactRepository extends GraphRepository<Contact> {
#Query("MATCH (a:BankAccount)-[r:HAS_CONTACT]->(:Person) " +
"WHERE ID(a)={accountId} " +
"RETURN r")
Iterable<Contact> findByAccountId(#Param("accountId") Long accountId);
}
After saving 1 account, 1 person and 1 contact relationship between them the below query is the one that fails:
Iterable<Contact> contacts = contactRepository.findByAccountId(accountId);
// this assertion will Fail for the BOLT driver, however, it will Succeed for the HTTP driver
// if the accountRepository.findOne(accountId) statement is executed before calling
// contactRepository.findByAccountId(accountId) then the test will also succeed for the BOLT driver
assertThat(size(contacts), is(1));
See below the response from the neo4j team about the issue:
The reason the relationship entity cannot be mapped is because the start and end node are unavailable with this query:
#Query("MATCH (a:BankAccount)-[r:HAS_CONTACT]->(p:Person) " +
"WHERE ID(a)={accountId} " +
"RETURN r,")
Iterable<Contact> findByAccountId(#Param("accountId") Long accountId);
You need to return these nodes to be able to construct a valid
relationship entity, like this:
#Query("MATCH (a:BankAccount)-[r:HAS_CONTACT]->(p:Person) " +
"WHERE ID(a)={accountId} " +
"RETURN r,a,p")
Iterable<Contact> findByAccountId(#Param("accountId") Long accountId);
It's a side effect of the HTTP endpoint that allows this to pass- the
start and end node are returned by this endpoint anyway. Please see
http://graphaware.com/neo4j/2016/04/06/mapping-query-entities-sdn.html
for more info.

Prevent Multiple Relationships between two nodes in Neo4J

I am building an application using Spring-Data-Neo4J (4.0.0.RELEASE). The application has two entities, a Person entity, and a Group entity. There is also a relationship entity, Member_Of, which says that a person is a member of a group. The entities look like below
#NodeEntity(label="Person")
public class Person {
protected String uuid;
protected String fullName;
protected String email;
#Relationship(type = RelationshipNames.MEMBER_OF, direction = Relationship.OUTGOING)
protected List<GroupMembership> groupMemberships = new ArrayList<GroupMembership>() ;
}
#NodeEntity(label="Group")
public class Group implements Serializable{
protected static final long serialVersionUID = 1L;
#GraphId
protected Long id;
protected String uuid;
protected String name;
#Relationship(type = RelationshipNames.MEMBER_OF, direction = Relationship.INCOMING)
protected List<GroupMembership> groupMemberships = new ArrayList<GroupMembership>() ;
}
#RelationshipEntity(type = RelationshipNames.MEMBER_OF)
public class GroupMembership implements Serializable{
private static final long serialVersionUID = 1L;
#GraphId Long id;
#Property String uuid;
#StartNode Person member;
#EndNode Group group;
#DateLong
Date date;
}
There is a method which adds members to a group. The method is annotated with #Transactional. The sample code is given below.
#Override
#Transactional(propagation=Propagation.REQUIRED)
public ResponseEntity<GroupVO> addMembersToGroup(String groupUuid, String personUuid, List<String> personsToAdd){
Group group = groupRepository.findGroupByUuid(groupUuid);
List<Person> personsToAdd = IterableUtils.toList(personRepository.findPersonsByUuid(personUuids));
personsToAdd.forEach(personToAdd -> {
GroupMembership existingGroupMembership = getActiveMembership(group.getUuid(), personToAdd.getUuid());
if(existingGroupMembership==null){
GroupMembership newGroupMembership = new GroupMembership(personToAdd, group, GroupRoleNames.MEMBER, member.getUuid(), new Date());
personToAdd.getGroupMemberships().add(newGroupMembership);
group.getGroupMemberships().add(newGroupMembership);
}
groupRepository.save(group);
}
What it tries to do is that, it searches for a existing relationship between the personToAdd and the group. If it returns null, that is, no relationship exists, it adds it to the group.
The problem is sometimes the same person is added multiple times to the same group. This is happenning when two people are running the application and both of them tries to add the same person to the same group.
How do I prevent this from happenning? I need to have a single relationship between a person and the group and not multiple ones.
There will be only one relationship created between two given entities provided all properties on the relationship entity are equal. You have a timestamp which is the culprit here- SDN realises that the two relationships differ because they have different values for a property and goes ahead and creates the second one.
At the moment, SDN does not have configuration to allow you to specify a merge vs create for relationship entities.
You'll probably have to manage some synchronization at the application level.
I had the same issue, and was able to "solve" it by using a custom cypher query. Given that you have added successfully Person and Group entity you can run the following query:
#Query("MATCH (group:Group) " +
"MATCH (person:Person) " +
"WHERE person.uuid={0} AND group.uuid={1} "+
"MERGE (person)-[r:MEMBER_OF]->(group) " +
"SET r.uuid ={2} , r.date={3} " +
"RETURN r")
GroupMembership isMemberOf(String personUuid,String groupUuid, String uuid, Date date);
by calling it like this:
personRepository.isMemberOf(personUuid,groupUuid,uuid,date);
However, don't take it for granted. I haven't done extensive tests to ensure that this approach is thread-safe.
This answer by William Lyon on atomic execution of MERGE may be an extra step that you have to take.

Followee <- Follower relationship Spring-data Neo4j

I have a domain model like Twitter where a USER can FOLLOW other USERs. My class looks like this,
#NodeEntity
public class UserNodeEntity {
#GraphId
Long id;
#Property(name = "firstName")
private String firstName;
#Property(name = "lastName")
private String lastName;
#Relationship(type = "FOLLOWS", direction = Relationship.INCOMING)
private Set<UserNodeEntity> followers = new HashSet<>();
#Relationship(type = "HAS_ROLE", direction = Relationship.OUTGOING)
private Set<UserRole> roles = new HashSet<>();
// getters and setters
...
}
My code to add a new follower looks like this,
public void createNewFollower(String id, String followerId) {
UserNodeEntity usr = getUserById(id); //fetch the user by id from Neo4j
UserNodeEntity follower = getUserById(followerId);
if (usr != null) {
usr.getFollowers().add(follower);
userRepo.save(usr);
}
}
And code to add new Role looks like this,
public void assignNewRole(String id, UserRole role) {
UserNodeEntity usr = getUserById(id); //fetch the user by id from Neo4j
if (usr != null) {
usr.getRoles().add(role);
userRepo.save(usr);
}
}
When I call the createNewFollower() method with the ids of follower and followee one relationship (FOLLOWEE <- FOLLOWS <- FOLLOWER) is created as expected. But when I call the assignNewRole() method with the id of user and his role a new relationship is created by name HAS_ROLE and one more FOLLOWS relationship is created between the previous follower and followee. Now the two users follow each other instead of one relationship (FOLLOWEE <- FOLLOWS <- FOLLOWER)
Can anyone help me in understanding why this is happening?
My Neo4j version is 2.3.3 and spring-data-neo4j version is 4.0.0.RELEASE

Resources