我要地图休眠以下
的错误我得到的,当我实现用户表:
无法设置由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
{
[试用] [1] 使用上面的链接,你可能会得到答案。 [1]:http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate –
@PrasannaKumarHA请参阅更新 – QGA