Reputation: 87
I'm using Spring Data Neo4j 4 (SDN4),And trying to return more than one entities using the @QueryResult
There is no exception thrown,but everything is null in the @QueryResult object The model is simple as (:User)-[:ROLE]-(:Role)
Does any one know what is the root cause?
The User model
@NodeEntity
public class User implements UserDetails {
@GraphId
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private String userId;
@Relationship(type="ROLE")
private Set<Role> roles = new HashSet<>();
public ADUser getaDUser() {
return aDUser;
}
public void setaDUser(ADUser aDUser) {
this.aDUser = aDUser;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
The Role Model
@NodeEntity
public class Role implements java.io.Serializable {
@GraphId Long id;
private String name;
public Role(){}
public Role(String name){
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
The QueryResult
@QueryResult
public class UserPermission {
private User user;
private List<Role> roles;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
The UserRepository
public interface UserRepository extends GraphRepository<User>{
public User findByUserId(String userId);
@Query(" Match (u:User)-[:ROLE]->(r) return u as user , collect(r) as roles ")
public UserPermission findUserWithRoles();
}
And the log is
2015-08-05 17:56:18 INFO Neo4jSession:461 - --------- new request ----------
2015-08-05 17:56:18 INFO Neo4jSession:461 - getOrCreateTransaction() being called on thread: 1
2015-08-05 17:56:18 INFO Neo4jSession:461 - Session identity: org.neo4j.ogm.session.delegates.TransactionsDelegate@1dcfae07
2015-08-05 17:56:18 INFO Neo4jSession:461 - There is no existing transaction, creating a transient one
2015-08-05 17:56:18 INFO DefaultRequest:57 - POST http://localhost:7474/db/data/transaction/commit, request: {"statements":[{"statement":"Match (u:User)-[:ROLE]->(r) return u as user , collect(r) as roles","parameters":{},"resultDataContents":["row"],"includeStats":false}]}
2015-08-05 17:56:18 INFO DefaultRequest:86 - Response is OK, creating response handler
2015-08-05 17:56:18 WARN SingleUseEntityMapper:97 - Unable to find property: roles on class: model.UserPermission for writing
2015-08-05 17:56:18 WARN SingleUseEntityMapper:97 - Unable to find property: user on class: model.UserPermission for writing
2015-08-05 17:56:18 INFO JsonResponse:103 - Closing HttpResponse
Upvotes: 2
Views: 1302
Reputation: 19373
There's no support at the moment to map entities into a @QueryResult
.
So instead of returning User
and Role
you'll have to return user id's and role id's and then hydrate them using load
for example.
Alternatively, if you're looking for a simple way to find users by roles, you might try a method such as
List<User> findByRolesName(String roleName)
on the UserRepository
Upvotes: 4