复合键中的 Hibernate @GeneratedValue
Hibernate @GeneratedValue in a composite key
问题:
我想在休眠中映射以下内容
我在实现用户 table 时遇到的错误是:
无法通过 User.id
的反射 setter 设置字段值
我正在使用 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 注释轻松解决问题,但这似乎不是正确的方法
你能帮我解决一下吗
更新
感谢@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;
}
}
注意:我还更新了我的用户 class 为
@Entity
@IdClass(UserPK.class)
@Table(name="users")
public class User implements Serializable
{
显然,当我在复合键中使用 GeneratedValue 时,会出现很多问题
删除 GeneratedValue 注释解决了我的问题
任何人都可以解释这是否足以让 mysql 自动递增选项正常工作
根据 Hibernate 中的当前实现,不可能在复合键中使用 @GeneratedValue,请选择复合键或单个 @GeneratedValue id。
在 Hibernate 中报告了带有自动生成部分的复合 ID 的问题 - HHH-6044
问题:
我想在休眠中映射以下内容
我在实现用户 table 时遇到的错误是:
无法通过 User.id
的反射 setter 设置字段值我正在使用 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 注释轻松解决问题,但这似乎不是正确的方法
你能帮我解决一下吗
更新
感谢@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;
}
}
注意:我还更新了我的用户 class 为
@Entity
@IdClass(UserPK.class)
@Table(name="users")
public class User implements Serializable
{
显然,当我在复合键中使用 GeneratedValue 时,会出现很多问题
删除 GeneratedValue 注释解决了我的问题
任何人都可以解释这是否足以让 mysql 自动递增选项正常工作
根据 Hibernate 中的当前实现,不可能在复合键中使用 @GeneratedValue,请选择复合键或单个 @GeneratedValue id。
在 Hibernate 中报告了带有自动生成部分的复合 ID 的问题 - HHH-6044