Reputation: 107
my structure is as follows:
a company -> having set of users -> each user is having set of policies -> each policy is for a company.
sample graph structure :
u1 <- c1 -> u2
p1 -> c1
u1 -> p1
company(c1) is having two users (u1, u2) and a policy(p1) belongs lo company(c1). "u1" has taken the policy(p1). "u2" has no policy.
When i'm trying to load the u2 by "loadByProperty" method, expected should be User2 object and the corresponding Company(c1).
But, the policy(p1) is also loading and setting to "u2" object.
Class Structure;
Company{
Stirng companyName;
@Relationship(type = "hasUsers", direction = Relationship.OUTGOING)
List<User> users;
}
User{
Stirng userName;
@Relationship(type = "hasPolicy", direction = Relationship.OUTGOING)
List<Policy> policies;
}
Policy{
String policyName;
@Relationship(type = "forCompany", direction = Relationship.OUTGOING)
Company company;
}
Method for retrieving User:
public User getUser(String userName) {
Property<String, Object> properties = new Property<String, Object("userName", userName);
Set<User> users = (Set<User>) session.loadByProperty(User.class, properties);
if (users != null && !users.isEmpty())
for (User user : users)
return user;
return null;
}
Using SDN 4 (mile stone version).
Any suggestions? Thanks is advance.
Upvotes: 2
Views: 212
Reputation: 19373
There are a number of ways in which you can load an entity by property value.
Neo4jOperations
Use loadByProperty
or loadAllByProperty
Derived finders (repositories only)
@Repository
public interface UserRepository extends GraphRepository<User> {
User findByName(String name);
}
SDN will use the metadata info to translate this into a Cypher query.
@Query (repositories only)
@Repository
public interface UserRepository extends GraphRepository<User> {
@Query("MATCH (user:User{name:{0}}) RETURN user")
User findUserByName(String name);
}
Session
Use any of the loadAll
methods which accept a Filter
.
Example:
session.loadAll(Person.class, new Filter("firstName", "John"))
Upvotes: 1