I have Department and UserRelation entity. Department entity has a departmentManager field that is tied with many to one relationship with UserRelation. The problem is i inserted the same user relation id to two different departments and now when i fetch the departments the log says More than one row with the given identifier was found: 14, for class: models.user.UserRelation
Here is the simplified version of UserRelation entity:
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Entity
@Table(name = "user_relations")
public class UserRelation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_relation_id")
private Long id;
@OneToOne
@JoinColumn(name = "user_id", nullable = false, unique = true)
@JsonIgnore
private User user;
@OneToMany(mappedBy = "departmentManager", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH})
@JsonIgnore
private List<Department> departmentManagers;
}
And this is the simplified version of Department entity:
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
@Entity
@Table(name = "departments")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "department_id")
private Long id;
@Column(name = "department_name", nullable = false)
private String departmentName;
@ManyToOne
@JoinColumn(name = "user_relation_id")
@JsonIgnoreProperties("department")
private UserRelation departmentManager;
}