Problem:
I want to map the following in hibernate
The error I get when I implement users table is:
Could not set a field value by reflection setter of User.id
I am using MySql and the field id is auto incremented
@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;
}
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
I can easily fix the problem using @Transient annotation on id but this does not seem the right approach
Can you help me in sorting this out please
UPDATE
Thanks to @Prasanna I created the following but still same problem
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;
}
}
Noted: I have also updated my User class with
@Entity
@IdClass(UserPK.class)
@Table(name="users")
public class User implements Serializable
{
As per current implementation in Hibernate, its not possible to use @GeneratedValue in a composite key, choose either a composite key or a single @GeneratedValue id.
There has been a reported issue in Hibernate for Composite ID with auto-generated part - HHH-6044
Apparently when I use GeneratedValue in a composite key many problems arise
Removing the GeneratedValue annotation solved my problem
Can anyone explain if this is enough for mysql auto increment option to work properly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With