Could you please let me know the problem in below code?
- Role entity class (for Role table) - in this class i have "Set userRoles" and in the getter of this userRoles i have @OneToMany(mappedBy = "role")
- User entity class (for User table) - in this class i have "Set userRoles" and in the getter of this userRoles i have @OneToMany(mappedBy = "user")
- UserRole entity class (for UserRole table) - in this class i have
3.1 "Role role" and in the getter of this role i have "@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "role_id")"
3.2 "User user" and in the getter of this user i have @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "user_id")"
3.3 Also "BigInteger userRoleId" is the primary key of this table
Now when i run this to find the user by name, i get the result json in which user json contains userRoles which contains role which again contains userRoles and this repetition of userRoles and roles goes on and on indefinitely. Could you please help me why this repetition is happening?
User entity
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private BigInteger userId;
private String userComment;
private BigInteger userCreatedBy;
private Date userCreatedOn;
private String userEmail;
private String userName;
private String userPassword;
private String userPhone;
private byte userStatus;
private BigInteger userUpdatedBy;
private Date userUpdatedOn;
private Set<UserRole> userRoles;
public User() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
public BigInteger getUserId() {
return this.userId;
}
public void setUserId(BigInteger userId) {
this.userId = userId;
}
@Column(name = "user_comment")
public String getUserComment() {
return this.userComment;
}
public void setUserComment(String userComment) {
this.userComment = userComment;
}
@Column(name = "user_created_by")
public BigInteger getUserCreatedBy() {
return this.userCreatedBy;
}
public void setUserCreatedBy(BigInteger userCreatedBy) {
this.userCreatedBy = userCreatedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "user_created_on")
public Date getUserCreatedOn() {
return this.userCreatedOn;
}
public void setUserCreatedOn(Date userCreatedOn) {
this.userCreatedOn = userCreatedOn;
}
@Column(name = "user_email")
public String getUserEmail() {
return this.userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
@Column(name = "user_name")
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "user_password")
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@Column(name = "user_phone")
public String getUserPhone() {
return this.userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
@Column(name = "user_status")
public byte getUserStatus() {
return this.userStatus;
}
public void setUserStatus(byte userStatus) {
this.userStatus = userStatus;
}
@Column(name = "user_updated_by")
public BigInteger getUserUpdatedBy() {
return this.userUpdatedBy;
}
public void setUserUpdatedBy(BigInteger userUpdatedBy) {
this.userUpdatedBy = userUpdatedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "user_updated_on")
public Date getUserUpdatedOn() {
return this.userUpdatedOn;
}
public void setUserUpdatedOn(Date userUpdatedOn) {
this.userUpdatedOn = userUpdatedOn;
}
// bi-directional many-to-one association to UserRole
@OneToMany(mappedBy = "user")
public Set<UserRole> getUserRoles() {
return this.userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
public UserRole addUserRole(UserRole userRole) {
getUserRoles().add(userRole);
userRole.setUser(this);
return userRole;
}
public UserRole removeUserRole(UserRole userRole) {
getUserRoles().remove(userRole);
userRole.setUser(null);
return userRole;
}
}
Role entity
@Entity
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
private BigInteger roleId;
private BigInteger createdBy;
private Date createdOn;
private String roleDescription;
private String roleName;
private byte status;
private BigInteger updatedBy;
private Date updatedOn;
private Set<UserRole> userRoles;
public Role() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "role_id")
public BigInteger getRoleId() {
return this.roleId;
}
public void setRoleId(BigInteger roleId) {
this.roleId = roleId;
}
@Column(name = "created_by")
public BigInteger getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(BigInteger createdBy) {
this.createdBy = createdBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
public Date getCreatedOn() {
return this.createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Column(name = "role_description")
public String getRoleDescription() {
return this.roleDescription;
}
public void setRoleDescription(String roleDescription) {
this.roleDescription = roleDescription;
}
@Column(name = "role_name")
public String getRoleName() {
return this.roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public byte getStatus() {
return this.status;
}
public void setStatus(byte status) {
this.status = status;
}
@Column(name = "updated_by")
public BigInteger getUpdatedBy() {
return this.updatedBy;
}
public void setUpdatedBy(BigInteger updatedBy) {
this.updatedBy = updatedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_on")
public Date getUpdatedOn() {
return this.updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
// bi-directional many-to-one association to UserRole
@OneToMany(mappedBy = "role")
public Set<UserRole> getUserRoles() {
return this.userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
public UserRole addUserRole(UserRole userRole) {
getUserRoles().add(userRole);
userRole.setRole(this);
return userRole;
}
public UserRole removeUserRole(UserRole userRole) {
getUserRoles().remove(userRole);
userRole.setRole(null);
return userRole;
}
}
UserRole entity
@Entity
@Table(name = "user_role")
public class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
private BigInteger userRoleId;
private String comment;
private BigInteger createdBy;
private Date createdOn;
private byte status;
private BigInteger updatedBy;
private Date updatedOn;
private Role role;
private User user;
public UserRole() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_role_id")
public BigInteger getUserRoleId() {
return this.userRoleId;
}
public void setUserRoleId(BigInteger userRoleId) {
this.userRoleId = userRoleId;
}
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
@Column(name = "created_by")
public BigInteger getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(BigInteger createdBy) {
this.createdBy = createdBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
public Date getCreatedOn() {
return this.createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public byte getStatus() {
return this.status;
}
public void setStatus(byte status) {
this.status = status;
}
@Column(name = "updated_by")
public BigInteger getUpdatedBy() {
return this.updatedBy;
}
public void setUpdatedBy(BigInteger updatedBy) {
this.updatedBy = updatedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_on")
public Date getUpdatedOn() {
return this.updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
// bi-directional many-to-one association to Role
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "role_id")
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
// bi-directional many-to-one association to User
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
Output json (some data has been removed for brevity)
{
"userName": "Brijesh Yadav",
"userRoles": [{
"status": 1,
"role": {
"roleName": "ROLE_TDR_ADMIN",
"userRoles": [{
"status": 1,
"role": {
"roleName": "ROLE_TDR_ADMIN",
"userRoles": [{
"status": 1,
"role": {
"roleName": "ROLE_TDR_ADMIN",
"userRoles": [{
// this goes on indefinitely
}
]
}
}
]
}
}
]
}
}
]
}