Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Update Many To One Property

I have an problem with saving Many-To-One Field in Spring Data JPA. Consider Entity User and Group as shortly described below :

    @Entity
    @Table(name = "Users")
    public class User {
        @Basic(optional = false)
        @Column(name = "username")
        private String username;

        @JoinColumn(name = "group_id", referencedColumnName = "id")
        @ManyToOne
        private Group group;
    }

    @Entity
    @Table(name = "groups")
    public class Group {
       @Basic(optional = false)
       @Column(name = "name")
       private String name;

       @OneToMany(mappedBy = "group",cascade=CascadeType.PERSIST)
       private Collection<User> userCollection;
    }

when i want to Update User Entity and change its group with CrudRepository save method following exception occured : org.hibernate.HibernateException: identifier of an instance of Group was altered from 2 to 1 and that shows spring data want to edit Group's id field while it is not corrent and i want to change reference of it not its id.

For Update i get view data in a DTO form and after that i convert dto object to entity object using Dozer Mapper as described below:

DozerBeanMapper mapper = new DozerBeanMapper();
// get user by id from database for editing
User user = this.userService.findByIdAndDeletedFalse(form.getId());
// merge view data and entity data using Dozer
mapper.map(form, user);
// save User entity
this.userService.save(user);

userService is a bean that call crudRepository save method only.

Any Solution?

Thanks

like image 724
Farid327 Avatar asked Oct 23 '25 10:10

Farid327


1 Answers

The error occurs because you are trying to change the id of a tracked entity, which is not allowed in JPA. You need to obtain a reference to the new Group and assign it to User.group.

In short, instead of this:

user.getGroup().setId(newId);

Try this:

user.setGroup(entityManager.getReference(Group.class, newId)); 
like image 80
crizzis Avatar answered Oct 25 '25 23:10

crizzis