@GeneratedValue(strategy = GenerationType.IDENTITY) 不适用于@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY) does not work with @EmbeddedId
我是 Hibernate 的新手,需要一些帮助。
以前我只有 1 个主键,我使用 @GeneratedValue(strategy = GenerationType.IDENTITY)
在我的数据库中创建一个序列。
现在,我已经使用 @EmbeddedId
和下面的新 Employee.java
更改为复合主键。
由于某种原因,增量排序不再有效。对于我所有的新条目,它只是保持在 0。
如果能对此做出一些澄清,我们将不胜感激。我怎样才能自动增加价值?
谢谢!
Employee.java - 新
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@EmbeddedId
private EmployeePK pk;
private String name;
private String username;
public Employee() {
}
public Employee(String username, String name) {
super();
this.pk = new EmployeePK(username);
this.name = name;
}
@Embeddable
public class EmployeePK implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int employeeId;
private String username;
public EmployeePK() {
}
public EmployeePK(String username) {
this.username = username;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + employeeId;
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;
EmployeePK other = (EmployeePK) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (employeeId != other.employeeId)
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
private Employee getOuterType() {
return Employee.this;
}
}
public int getEmployeeId() {
return pk.getEmployeeId();
}
public String getUsername() {
return pk.getUsername();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@GeneratedValue
JavaDoc 提到:
The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.
然而,根据其文档 (2.2.3.2.4. Partial identifier generation),Hibernate 可能支持此类构造。还有一个示例代码,请随意尝试。文档的这一部分还包含以下警告:
The Hibernate team has always felt such a construct as fundamentally wrong. Try hard to fix your data model before using this feature.
因此,也许更好的解决方案是重新考虑模型,而不是尝试让它发挥作用(如果可以的话)。
我是 Hibernate 的新手,需要一些帮助。
以前我只有 1 个主键,我使用 @GeneratedValue(strategy = GenerationType.IDENTITY)
在我的数据库中创建一个序列。
现在,我已经使用 @EmbeddedId
和下面的新 Employee.java
更改为复合主键。
由于某种原因,增量排序不再有效。对于我所有的新条目,它只是保持在 0。
如果能对此做出一些澄清,我们将不胜感激。我怎样才能自动增加价值?
谢谢!
Employee.java - 新
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@EmbeddedId
private EmployeePK pk;
private String name;
private String username;
public Employee() {
}
public Employee(String username, String name) {
super();
this.pk = new EmployeePK(username);
this.name = name;
}
@Embeddable
public class EmployeePK implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int employeeId;
private String username;
public EmployeePK() {
}
public EmployeePK(String username) {
this.username = username;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + employeeId;
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;
EmployeePK other = (EmployeePK) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (employeeId != other.employeeId)
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
private Employee getOuterType() {
return Employee.this;
}
}
public int getEmployeeId() {
return pk.getEmployeeId();
}
public String getUsername() {
return pk.getUsername();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@GeneratedValue
JavaDoc 提到:
The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.
然而,根据其文档 (2.2.3.2.4. Partial identifier generation),Hibernate 可能支持此类构造。还有一个示例代码,请随意尝试。文档的这一部分还包含以下警告:
The Hibernate team has always felt such a construct as fundamentally wrong. Try hard to fix your data model before using this feature.
因此,也许更好的解决方案是重新考虑模型,而不是尝试让它发挥作用(如果可以的话)。