开发者

Problem with update id

开发者 https://www.devze.com 2023-03-13 15:18 出处:网络
I\'m gonna try to explain my problemas well I can I have some user groups to manage user rights. Some users may be customers. There\'s a OneToOne connection between the user table and the customers t

I'm gonna try to explain my problem as well I can I have some user groups to manage user rights.

Some users may be customers. There's a OneToOne connection between the user table and the customers table. And a OneToOne connection between users and groups. When I delete a client, I would like the user group changes from customer to user, the default group (Id=4).

With this code:

    public static void delete(Long id) {
       Customer entity = Customer.findById(id);
       User entityUser = User.findById(entity.user.id);

       entity.delete();

       entityUser.groups.id = (long)4;

       entityUser.merge();
       entityUser.save();

       flash.success(Messages.get("Users.deleted"));
       Customers.list();

   }

Group model:

    @Entity
@Table(name = "groups")
public class Group  extends Model{

   @Required
          public String name;
   @Required
   public String description;
   public Group(String name, String description)
   {
       this.name = name;
       this.description = description;
   }

User model:

  @Entity
@Table(name = "users")
public class User extends Model{


   @Required
           public String firstname;
   @Required
           public String lastname;
   @As("dd/MM/yyyy")
   @Required
           public Date birthday;
   @Required
           public String avatar;
   @Required
           public String adress;
   @Required
           public String phonenumber;
   @Required
   @Email
           public String email;
   @Required
           public String username;
   @Required
   @Password
           public String password;
   @OneToOne
   @Requ开发者_如何转开发ired
           public Group groups;

   public User(
           String firstname,
           String lastname,
           @As("dd/MM/yyyy") Date birthday,
           String avatar,
           String adress,
           String phonenumber,
           String email,
           String username,
           String password,
           Group groups
           )
   {
       if(groups == null){
           groups.id = (long)4;
       }
       else
       {
          this.groups = groups;
       }
       this.firstname = firstname;
       this.lastname = lastname;
       this.birthday = birthday;
       this.avatar = avatar;
       this.adress = adress;
       this.phonenumber = phonenumber;
       this.email = email;
       this.username = username;
       this.password = password;




   }

Customer model:

 @Entity
@Table(name = "customers")
public class Customer extends Model{

   @As("dd/MM/yyyy")
   public Date dateinscription;
   public Double amountdue;
   public Double amountpaid;
   @Required
   public String picture;
   @OneToOne
           public User user;

   public Customer(@As("dd/MM/yyyy") Date dateinscription, Double amountdue, Doubleamountpaid, String picture, User user)
   {
       this.dateinscription = dateinscription;
       this.amountdue = amountdue;
       this.amountpaid = amountpaid;
       this.picture = picture;
       this.user = user;


   }


}

But I've got an error:

PersistenceException occured : org.hibernate.HibernateException: identifier of an instance of models.Group was altered from 3 to 4

In /app/controllers/Customers.java (around line 69) 65:

66: entityUser.groups.id = (long)4; 67:

68: entityUser.merge(); 69: entityUser.save(); 70:

71: flash.success(Messages.get("Users.deleted")); 72: Customers.list(); 73:

74: } 75:

I tried with GenericModel but it didn’t work.

Please help me !!

Thank you.


In your code, you are explicitly changing the ID of the group, not the group. Hibernate assumes you want to change that field. But when you try to save the change, it won't let you, since it's the ID. It doesn't assume you mean "change the group to the one that has an ID of 4."

Instead, you'll need to load the default group, and set the user's group that that group.

Group group = Group.findById(4);
entityUser.groups = group;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号