Issue
I am new to Hibernate and need some help here.
Previously I had only 1 primary key and I used the @GeneratedValue(strategy = GenerationType.IDENTITY)
to create a sequence in my database.
Now, I have changed to composite primary keys using @EmbeddedId
and below my new Employee.java
.
For some reason, the increment sequencing no longer works. It just stays at 0 for all my new entries.
Some clarification of this would be most appreciated. And how can I go about automatically incrementing the value?
Thank you!
Employee.java - NEW
@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;
}
}
Solution
@GeneratedValue
JavaDoc mentions:
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.
However Hibernate probably supports such constructs according to their documenation (2.2.3.2.4. Partial identifier generation). There is also an example code, so feel free to give it a try. This section of the documentation also contains following warning:
The Hibernate team has always felt such a construct as fundamentally wrong. Try hard to fix your data model before using this feature.
So maybe better solution would be to rethink the model rather than to try to get this to work (if you can).
Answered By - Bohuslav Burghardt
Answer Checked By - Senaida (JavaFixing Volunteer)