2015-04-17 175 views
0

问题:休眠@GeneratedValue复合键

我要地图休眠以下enter image description here

的错误我得到的,当我实现用户表:

无法设置由User.id的反射设置器的字段值

我正在使用MySql并且字段ID是自动递增的

@Entity 
@Table(name="users") 
public class User implements Serializable 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue 
    private Integer id; 

    @Id 
    private String username; 

    //Other fields 
    public User() 
    { 

    } 

    public User(String username, String email, String firstName, 
      String lastName, String password, String authority, 
      boolean enabled, boolean reset, boolean deleted) 
    { 
     this.username = username; 
     this.email = email; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.password = password; 
     this.authority = authority; 
     this.enabled = enabled; 
     this.reset = reset; 
     this.deleted = deleted; 
    } 

    public User(int id, String username, String email, String firstName, 
      String lastName, String password, String authority, 
      boolean enabled, boolean reset, boolean deleted) 
    { 
     this.id = id; 
     this.username = username; 
     this.email = email; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.password = password; 
     this.authority = authority; 
     this.enabled = enabled; 
     this.reset = reset; 
     this.deleted = deleted; 
    } 

    public int getId() 
    { 
     return id; 
    } 

    public void setId(int id) 
    { 
     this.id = id; 
    } 

的问题是,在休眠状态,如果我想使用两个主键实体的我不得不使用注释@Embedded,但我不知道如何使用它正确

我可以轻松地修复但这似乎使用的ID @Transient批注问题不正确的方法

你能帮我在整理了这一点请

UPDATE

感谢@Prasanna我创建了以下,但仍同样的问题

public class UserPK implements Serializable 
{ 
/** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 


    protected Integer id; 
    protected String username; 

    public UserPK() 
    { 

    } 

    public UserPK(Integer id, String username) 
    { 
     this.id = id; 
     this.username = username; 
    } 

    @Override 
    public int hashCode() 
    { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((id == null) ? 0 : id.hashCode()); 
     result = prime * result 
       + ((username == null) ? 0 : username.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) 
    { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     UserPK other = (UserPK) obj; 
     if (id == null) { 
      if (other.id != null) 
       return false; 
     } else if (!id.equals(other.id)) 
      return false; 
     if (username == null) { 
      if (other.username != null) 
       return false; 
     } else if (!username.equals(other.username)) 
      return false; 
     return true; 
    } 
} 

注意到:我也更新了我的用户类

@Entity 
@IdClass(UserPK.class) 
@Table(name="users") 
public class User implements Serializable 
{ 
+0

[试用] [1] 使用上面的链接,你可能会得到答案。 [1]:http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate –

+0

@PrasannaKumarHA请参阅更新 – QGA

回答

0

显然,当我在一个复合键的许多问题使用GeneratedValue出现

卸下GeneratedValue注解解决我的问题

任何人都可以,如果这个解释足以让mysql自动递增选项正常工作

1

根据Hibernate中的当前实现,不可能在组合键中使用@GeneratedValue,请选择组合键或单个@GeneratedValue id。

目前已在Hibernate的一个报告的问题为复合ID系统自动产生的部分 - HHH-6044

+0

这让我担心,谢谢您的反馈 – QGA