2015-04-17 202 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