I have two entities/tables
Removed some columns for brevity
Group:
@Entity
@Table(name = "groups", indexes = {@Index(name = "groups_index", columnList = "id", unique = true)})
public class Group implements Serializable {
@OneToMany(targetEntity = UserInGroup.class, mappedBy = "group")
private Set<UserInGroup> userInGroups = new HashSet<>();
}
UserInGroup:
@Entity
@Table(name = "user_in_group", indexes = {@Index(name = "user_in_group_index", columnList = "group_id,user_id", unique = true)})
public class UserInGroup implements Serializable {
@ManyToOne
@JoinColumn(name = "group_id", referencedColumnName = "id")
private Group group;
}
I have referred to these links 1, 2 and 3. I have done what they have outlined
Now I make an entry in the UserInGroup table, and still, the group entity doesn't get updated with the new entries made in UserInGroup table.
Group is not updated
Entry created in Groups
Edit:
Based on this answer I added the following
UserInGroup userInGroup = new UserInGroup().setGroup(group).setUser(creatorUser).setGroupRoleEnum(GroupRoleEnum.ADMIN);
group.addUserInGroup(userInGroup);
groupRepository.save(group);
public void addUserInGroup(UserInGroup userInGroup) {
if (userInGroups == null) {
userInGroups = new ArrayList<>();
}
userInGroups.add(userInGroup);
userInGroup.setGroup(this);
}
But the table UserInGroup still has no entries created.