I have 3 classes, there are User, Role and UserRole. I want to fetch the data of UserRole, but always return NULL.
This is User Class.
@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;
}
}
And this is my Role Class
@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();
}
}
And this is my 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();
}
}
I want to show all the User data, but the result always show userRoles NULL. I am using findAll(1) and findAll(2) but nothing different.
Its different from SDN 3, we only add @Fetch to the field. In SDN 4, no @Fetch annotation. How i can fetch all the data?
UPDATE!!
I changed the class like Luanne answer, and modified the User class like this :
@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();
}
}
After save the data, and show it, the Roles are shown like this :
[{"id":27,"username":"daviduck123","password":"admin","firstName":"David","lastName":"Vincent","role":"admin","active":null,"roles":{"id":"29","name":"ROLE_ADMIN"}}]
But after that I try to show again all the data, and its back to NULL again like this :
[{"id":27,"username":"daviduck123","password":"admin","firstName":"David","lastName":"Vincent","role":"admin","active":null,"roles":null}]
This is my repository https://github.com/daviduck123/neo4j-ogm-infiniteloop