title: Hibernate @GeneratedValue in a composite key
body: <p><strong>Problem:</strong></p>
  
  <p>I want to map the following in hibernate<img src="https://i.stack.imgur.com/wrsYt.png" alt="enter image description here"></p>
  
  <p>The error I get when I implement users table is:</p>
  
  <p><strong>Could not set a field value by reflection setter of User.id</strong></p>
  
  <p>I am using MySql and the field id is auto incremented</p>
  
  <pre><code>@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;
      }
  </code></pre>
  
  <p>The problem is that in hibernate if I wish to use two primary keys for an Entity I have to use the annotation @Embedded but I am not sure  how to use it properly</p>
  
  <p>I can easily fix the problem using @Transient annotation on id but this does not seem the right approach</p>
  
  <p>Can you help me in sorting this out please </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks to @Prasanna I created the following but still same problem</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>Noted: I have also updated my User class with</p>
  
  <pre><code>@Entity
  @IdClass(UserPK.class)
  @Table(name="users")
  public class User implements Serializable
  {
  </code></pre>