更新ID问题

时间:2011-06-17 19:52:13

标签: mysql hibernate java-ee playframework

我也会尝试解释我的问题 我有一些用户组来管理用户权限。

有些用户可能是客户。用户表和customers表之间存在OneToOne连接。用户和组之间的OneToOne连接。当我删除客户端时,我希望用户组从客户更改为用户,即默认组(Id = 4)。

使用此代码:

    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();

   }

小组模特:

    @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;
   }

用户模型:

  @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
   @Required
           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;




   }

客户模式:

 @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;


   }


}

但我有一个错误:

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

在/app/controllers/Customers.java(第69行) 65:

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

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

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

74:      } 75:

我尝试使用GenericModel但它没有用。

请帮助我!!

谢谢。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您明确更改了组的ID,而不是组。 Hibernate假设您想要更改该字段。但是当你试图保存更改时,它不会让你,因为它是ID。它并不假设您的意思是“将组更改为ID为4的组。”

相反,您需要加载默认组,并设置该组的用户组。

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