Cui Pengfei (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiY2I3NTA4MGY0...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16054?atlOrigin=eyJpIjoiY2I3NT...
) HHH-16054 (
https://hibernate.atlassian.net/browse/HHH-16054?atlOrigin=eyJpIjoiY2I3NT...
) 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=eyJpIjoiY2I3NT...
)
Issue Type: Bug Affects Versions: 5.6.14 Assignee: Unassigned Components: hibernate-core
Created: 17/Jan/2023 06:12 AM Environment: Hibernate libraries:
hibernate-core-5.6.14.Final.jar
hibernate-commons-annotations-5.1.2.Final.jar
JVM:
openjdk 16
operating system,
mac 12.6.2 and windows 10
database platform
pg and mysql
Priority: Major Reporter: Cui Pengfei (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
***************************
How to reproduce this issue
***************************
https://github.com/cuipengfei/Spikes/tree/master/jpa/ClassIdUpdateIssue (
https://github.com/cuipengfei/Spikes/tree/master/jpa/ClassIdUpdateIssue )
this code can reproduce the issue, just run the main method then the error will happen.
*****************
Issue Description
*****************
There is a base class like this:
@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 ......
}
Its IdClass is like this:
@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;
}
}
Then it has a subclass:
@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;
}
}
The subclass only adds one additional field, nothing else fancy.
Then when I try to persist an instance of the subclass like this:
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
*********
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.
************
The question
************
What is the root reason of this issue? Is @IdClass not supposed to be used for this
scenario?
*****
links
*****
https://stackoverflow.com/questions/75147518/jpa-hibernate-duplicate-pkey...
(
https://stackoverflow.com/questions/75147518/jpa-hibernate-duplicate-pkey...
)
(
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 )