3

I have the following code:

groupModel.getUserFormGroups().clear();
for(MemberDTO member : group.getMembers()){
    User u = userRepository.findByEmail(member.getEmail());
    System.out.println(member.getEmail() + " " + groupModel.getName() + " " + member.getRole());
    if(u == null){
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    groupModel.getUserFormGroups().add(new UserFormGroup(u, groupModel, UserFormGroupRole.ADMIN));
}

try{
    groupRepository.save(groupModel);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (DataIntegrityViolationException e){
    System.out.println(e.getMessage());
    System.out.println(e.getClass());
    return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
}

When I run this, the new UserFormGroups have an id and all the other fields are null. Is there something wrong with fully updating a ManyToOne relationship?

On the group entity I have the following OneToMany relation:

@OneToMany(targetEntity=UserFormGroup.class, cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.EAGER, mappedBy = "formGroup", orphanRemoval=true)
private Set<UserFormGroup> userFormGroups = new HashSet<>();

And the UserFormGroup relation looks like this:

@Entity
@Table(uniqueConstraints={
        @UniqueConstraint(columnNames = {"user", "form_group"})
})
public class UserFormGroup implements Serializable{

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    @ManyToOne
    @JoinColumn(name = "user",referencedColumnName = "id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "form_group", referencedColumnName = "id")
    private FormGroup formGroup;

    @Column(name = "role")
    private UserFormGroupRole role;

    public UserFormGroup() {
    }

    public UserFormGroup(User user, FormGroup group, UserFormGroupRole role) {
        this.user = user;
        this.formGroup = group;
        this.role = role;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public FormGroup getFormGroup() {
        return formGroup;
    }

    public void setFormGroup(FormGroup formGroup) {
        this.formGroup = formGroup;
    }

    public UserFormGroupRole getRole() {
        return role;
    }

    public void setRole(UserFormGroupRole role) {
        this.role = role;
    }
}
2
  • is groupRepository a JpaRepository? And to be clear, the UserFormGroup entities are created but with just the id and foreign key to the GroupModel? Commented Mar 24, 2017 at 11:17
  • More like a spring data repository, it extends CrudRepository. They are created but only the id is set, role is null, form_group is null and user is null. Commented Mar 24, 2017 at 11:28

1 Answer 1

3

Not 100% but in my opinion, the problem might be following:

The CrudRepository's implementation of the save method checks whether the object you are saving is a new or existing entity. If it is already an existing entity, it performs a merge operation. This would be the scenario that's happening in your case as the groupModel is an existing entity.

Now on the @OneToMany dependency, you only have these cascade options:

cascade = { CascadeType.PERSIST, CascadeType.REMOVE }

If you add CascadeType.MERGE the operation should be propagated to the UserFromGroup entities and persist them (the default behavior of merge when the entities are new ones).

Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work but I have the following error Duplicate entry '1-8' for key when I add a UserFormGroup that was already into the FormGroup. Probably because the orphan is deleted after the new one is created
Fixed it by retrieving the UserFormGroup if it already exists and creating it if it doesn't. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.