问题:
我要地图休眠以下
当我实现用户表是我的错误:
无法设置由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
{