在复合键中休眠@GeneratedValue [英] Hibernate @GeneratedValue in a composite key
问题描述
问题:
我想在 hibernate 中映射以下内容
I want to map the following in hibernate
我在实现用户表时得到的错误是:
The error I get when I implement users table is:
无法通过 User.id 的反射设置器设置字段值
我使用的是 MySql 并且字段 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;
}
问题是,在休眠中,如果我想为实体使用两个主键,我必须使用注释@Embedded,但我不确定如何正确使用它
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
我可以在 id 上使用 @Transient 注释轻松解决问题,但这似乎不是正确的方法
I can easily fix the problem using @Transient annotation on id but this does not seem the right approach
你能帮我整理一下吗
更新
感谢@Prasanna 我创建了以下但仍然是同样的问题
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;
}
}
注意:我还用
@Entity
@IdClass(UserPK.class)
@Table(name="users")
public class User implements Serializable
{
推荐答案
显然当我在复合键中使用 GeneratedValue 时会出现很多问题
Apparently when I use GeneratedValue in a composite key many problems arise
删除 GeneratedValue 注释解决了我的问题
Removing the GeneratedValue annotation solved my problem
谁能解释一下这是否足以使 mysql 自动增量选项正常工作
Can anyone explain if this is enough for mysql auto increment option to work properly
这篇关于在复合键中休眠@GeneratedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!