我有3个类,有User,Role和UserRole。我想获取UserRole的数据,但总是返回NULL。
这是用户类。
@NodeEntity
public class User {
@GraphId
Long id;
@NotNull
private String username;
@NotNull
private String password;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
private String role;
@NotNull
private Boolean active;
@Relationship(type = "HAS_ROLE", direction = Relationship.OUTGOING)
private Set<UserRole> userRoles;
public User() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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 getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public Iterable<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
}
这是我的角色等级
@NodeEntity
public class Role {
@GraphId
Long id;
@NotNull
String name;
@Relationship(type = "HAS_ROLE",direction = Relationship.OUTGOING)
@JsonIgnore
private Set<UserRole> userRoles;
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;
}
public Set<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
public Role() {
super();
}
}
这是我的UserRole Class
@RelationshipEntity(type = "HAS_ROLE")
public class UserRole {
@GraphId
Long id;
@StartNode
@Relationship(type="HAS_ROLE", direction=Relationship.INCOMING)
User user;
@EndNode
@Relationship(type="HAS_ROLE", direction=Relationship.INCOMING)
Role role;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public UserRole(User user, Role role) {
super();
this.user = user;
this.role = role;
}
public UserRole() {
super();
}
}
我想显示所有用户数据,但结果总是显示userRoles为NULL。我正在使用findAll(1)和findAll(2),但没有什么不同。
它与SDN 3不同,我们只在字段中添加@Fetch。在SDN 4中,没有@Fetch注释。我如何获取所有数据?
UPDATE !!
我像Luanne一样更改了类,并修改了这样的User类:
@NodeEntity
public class User {
@GraphId
Long id;
@NotNull
private String username;
@NotNull
private String password;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
private String role;
@NotNull
private Boolean active;
@Relationship(type = "HAS_ROLE")
private Set<UserRole> userRoles;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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 getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
@Relationship(type = "HAS_ROLE")
public Set<UserRole> getUserRoles() {
return userRoles;
}
@Relationship(type = "HAS_ROLE")
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
public User(){
super();
}
}
保存数据并显示后,角色显示如下:
[{"id":27,"username":"daviduck123","password":"admin","firstName":"David","lastName":"Vincent","role":"admin","active":null,"roles":{"id":"29","name":"ROLE_ADMIN"}}]
但之后我尝试再次显示所有数据,并再次返回NULL,如下所示:
[{"id":27,"username":"daviduck123","password":"admin","firstName":"David","lastName":"Vincent","role":"admin","active":null,"roles":null}]
这是我的资料库https://github.com/daviduck123/neo4j-ogm-infiniteloop
答案 0 :(得分:2)
@RelationshipEntity表示两个节点之间的合格关系,因此在其中使用@Relationship是不正确的。
UserRole
应该看起来像
@RelationshipEntity(type = "HAS_ROLE")
public class UserRole {
@GraphId
Long id;
@StartNode
User user;
@EndNode
Role role;
由于您要定义Role
到HAS_ROLE
之间的User
关系,因此Role
类中的关系方向应该是INCOMING:
@Relationship(type = "HAS_ROLE",direction = Relationship.INCOMING)
@JsonIgnore
private Set<UserRole> userRoles;
@Relationship(type = "HAS_ROLE",direction = Relationship.INCOMING)
public Set<UserRole> getUserRoles() {
return userRoles;
}
@Relationship(type = "HAS_ROLE",direction = Relationship.INCOMING)
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
确保访问者和mutator方法也被注释(当你有INCOMING关系时是强制性的。)
答案 1 :(得分:0)
我猜Luanne的解决方案可行,我们应该使用它。但我还有另一项工作要做。 您可以在存储库中使用@Query来自定义查询。在查询中,您可以返回所需的所有对象并将其记录到地图中。
@Query("match (ac: Address) <-[:at]- (c: Company) <-[:of]- (p: Position) -[:at]-> (a: Address) "
+ "where p.name =~ {0} return p as position, c as company, a as address, ac as companyAddress skip {1} limit {2}")
List<Map<String, Object>> fetchByTitle(String title, int start, int pageSize);
然后,您需要手动设置关系。
List<Position> positions = new ArrayList<Position>();
for (Map<String, Object> d : data) {
Position pos = (Position) d.get("position");
pos.setCompany((Company) d.get("company"));
pos.setLocation((Address) d.get("address"));
pos.getCompany().setAddress((Address) d.get("companyAddress"));
positions.add(pos);
}
return positions;
亲们不需要使用@RelationshipEntity,作为一个SQL人员,我仍然不习惯。 可以肯定的是,这不是一种自然解决方案。