我要地圖休眠以下
的錯誤我得到的,當我實現用戶表:
無法設置由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