Cui Pengfei (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNDQxY2UyNGQw...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16054?atlOrigin=eyJpIjoiNDQxY2...
) HHH-16054 (
https://hibernate.atlassian.net/browse/HHH-16054?atlOrigin=eyJpIjoiNDQxY2...
) JPA / Hibernate, duplicate pkey error when updating entity that is a subclass of a base
class that uses IdClass for composite primary key (
https://hibernate.atlassian.net/browse/HHH-16054?atlOrigin=eyJpIjoiNDQxY2...
)
Change By: Cui Pengfei (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
h1. # How to reproduce this issue
[ <strike>
https://github.com/cuipengfei/Spikes/tree/master/jpa/ClassIdUpdateIssue
|https://github.com/cuipengfei/Spikes/tree/master/jpa/ClassIdUpdateIssue|smart-link]
~~
this code can reproduce the issue, just run the main method then the error will happen.
</strike>
https://github.com/gregturn/spring-data-jpa-id-class-issues/tree/main/src...
Just make sure you have docker installed and run these 👆 unit tests.
The unit test with **EmbeddedId** will be ok.
But the one with **IdClass** will **fail**, while we **expect** it to be **successful**.
h1. Issue Description
There is a base class like this:
{code:java}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("case when vip_number is not null then 'vip' else
'normal' end")
@DiscriminatorValue("normal")
@IdClass(CustomerPK.class)
public class CustomerWithIdClass implements Serializable {
private String firstName;
private String lastName;
@Id
private Long versionId;
@Id
private Long unitId;
protected CustomerWithIdClass() {
}
// getter and setters ......
}{code}
Its IdClass is like this:
{code:java}@NoArgsConstructor
@EqualsAndHashCode
@Embeddable
public class CustomerPK implements Serializable {
private Long unitId;
private Long versionId;
public void setUnitId(Long unitId) {
this.unitId = unitId;
}
public void setVersionId(Long versionId) {
this.versionId = versionId;
}
}{code}
Then it has a subclass:
{code:java}@Entity
@DiscriminatorValue("vip")
public class VipCustomerWithIdClass extends CustomerWithIdClass {
private String vipNumber;
public VipCustomerWithIdClass() {
}
public VipCustomerWithIdClass(String firstName, String lastName, String vipNumber) {
super(firstName, lastName);
this.vipNumber = vipNumber;
}
public String getVipNumber() {
return vipNumber;
}
public void setVipNumber(String vipNumber) {
this.vipNumber = vipNumber;
}
}
{code}
The subclass only adds one additional field, nothing else fancy.
Then when I try to persist an instance of the subclass like this:
{code:java}Â Â Â Â Â Â Â Â CustomerWithIdClass customer = new CustomerWithIdClass("a",
"b");
customer.setVersionId(123L);
customer.setUnitId(456L);
repository.save(customer);//save object of base class, ok
customer.setFirstName("a2");
repository.save(customer);//modify object of base class and save again, ok
VipCustomerWithIdClass vipCustomer = new VipCustomerWithIdClass("a",
"b", "888");
vipCustomer.setVersionId(987L);
vipCustomer.setUnitId(654L);
repository.save(vipCustomer);//save object of subclass, ok
vipCustomer.setVipNumber("999");
repository.save(vipCustomer);//modify object of subclass and save again, NOT OK
// ↑ THIS FAILS BECAUSE OF PRIMARY KEY CONFLICT. INSERT STATEMENT WAS USED INSTEAD OF
UPDATE, WHY?
// this failure only happens when:
// 1. base class uses IdClass for composite primary key
// 2. saving an instance of the subclass for the second time after modification
{code}
h1. The Error
Then there will be an *error of duplicate pkey* when I try to save the instance of the
*subclass* for the *second time* *after some modification*.
The error seems to be related with the @*IdClass* annotation, because I have tried using
@*EmbeddedId* and @*Embeddable* for composite pkey, then the error does not happen.
h1. The question
What is the root reason of this issue? Is @IdClass not supposed to be used for this
scenario?
h1. links
[
https://stackoverflow.com/questions/75147518/jpa-hibernate-duplicate-pkey...]
[
https://github.com/spring-projects/spring-data-jpa/issues/2767|https://gi...]
(
https://hibernate.atlassian.net/browse/HHH-16054#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16054#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100214- sha1:771690a )