[JIRA] (HHH-16821) Fail to delete enty with a composite id using an @IdClass with one of its fields mapped from the id of a @ManyToOne association
by Erwan Moutymbo (JIRA)
Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMDIxNjg3NjMx... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiMDIxNj... ) HHH-16821 ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiMDIxNj... ) Fail to delete enty with a composite id using an @IdClass with one of its fields mapped from the id of a @ManyToOne association ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiMDIxNj... )
Change By: Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... )
h3. Description
I'm migrating from hibernate 5.6.15 to hibernate 6.2.5 and I have noticed some issues when I try to delete an entity that have a compositeId with one of its id field part of a @ManyToOne association.
h3. Entities
{code:java}@Getter
@IdClass(ProductPK.class)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)
@NoArgsConstructor(access = PROTECTED)
@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
@Cacheable
@Cache(usage = READ_WRITE)
@Table(name = "PRODUCTS")
public class Product {
public Product(String productId, Operator operator) {
this.productId = productId;
this.operator = operator;
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "PRODUCT_ID", nullable = false)
private String productId;
@Id
@EqualsAndHashCode.Include
@ToString.Include
@Getter
@Setter
@Cache(usage = READ_WRITE)
@ManyToOne(fetch = LAZY, optional = false)
@JoinColumn(nullable = false)
private Operator operator;
@Column(name = "DESCRIPTION")
@Setter
private String description;
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@NoArgsConstructor(access = PUBLIC)
public static class ProductPK implements Serializable {
private String productId;
private String operator;
}
}
{code}
{code:java}
@Getter
@Entity
@ToString(onlyExplicitlyIncluded = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@NoArgsConstructor(access = PROTECTED)
@Table(name = "OPERATORS")
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
@Cacheable
@Cache(usage = READ_WRITE)
public class Operator {
public Operator(String operatorId) {
this.operatorId = operatorId;
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "OPERATOR_ID", nullable = false)
private String operatorId;
@OneToMany(mappedBy = "operator", cascade = {CascadeType.ALL}, orphanRemoval = true, fetch = FetchType.LAZY)
private List<Product> products = new ArrayList<>();
public void setProducts(List<Product> products) {
this.products = products;
}
}{code}
h3. Test
{ noformat code:java } @Test
void shouldDeleteProduct() {
// Given
String string = "ID2";
String operatorID = "operatorID2";
String test = "test";
Operator operator = new Operator(operatorID);
operatorService.addOperator(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
// When
ProductPK productPK = new ProductPK(string, operatorID);
productService.deleteProduct(productPK);
// Then
Optional<Product> byId2 = productService.getProduct(productPK);
assertThat(byId2).isEmpty();
}{ noformat code }
I get the following exception :
{noformat}org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; statement executed: delete from products where operator_operator_id=? and product_id=? and operator_operator_id is null and product_id is null and description=?
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:307)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:232){noformat}
( https://hibernate.atlassian.net/browse/HHH-16821#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16821#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=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100227- sha1:8ffa416 )
1 year, 3 months
[JIRA] (HHH-16821) Fail to delete enty with a composite id using an @IdClass with one of its fields mapped from the id of a @ManyToOne association
by Erwan Moutymbo (JIRA)
Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZWIyZDQ4NWQ2... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiZWIyZD... ) HHH-16821 ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiZWIyZD... ) Fail to delete enty with a composite id using an @IdClass with one of its fields mapped from the id of a @ManyToOne association ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiZWIyZD... )
Change By: Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... )
h3. Description
I'm migrating from hibernate 5.6.15 to hibernate 6.2.5 and I have noticed some issues when I try to delete an entity that have a compositeId with one of its id field part of a @ManyToOne association.
h3. Entities
{code:java}@Getter
@IdClass(ProductPK.class)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)
@NoArgsConstructor(access = PROTECTED)
@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
@Cacheable
@Cache(usage = READ_WRITE)
@Table(name = "PRODUCTS")
public class Product {
public Product(String productId, Operator operator) {
this.productId = productId;
this.operator = operator;
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "PRODUCT_ID", nullable = false)
private String productId;
@Id
@EqualsAndHashCode.Include
@ToString.Include
@Getter
@Setter
@Cache(usage = READ_WRITE)
@ManyToOne(fetch = LAZY, optional = false)
@JoinColumn(nullable = false)
private Operator operator;
@Column(name = "DESCRIPTION")
@Setter
private String description;
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@NoArgsConstructor(access = PUBLIC)
public static class ProductPK implements Serializable {
private String productId;
private String operator;
}
}
{code}
{code:java}
@Getter
@Entity
@ToString(onlyExplicitlyIncluded = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@NoArgsConstructor(access = PROTECTED)
@Table(name = "OPERATORS")
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
@Cacheable
@Cache(usage = READ_WRITE)
public class Operator {
public Operator(String operatorId) {
this.operatorId = operatorId;
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "OPERATOR_ID", nullable = false)
private String operatorId;
@OneToMany(mappedBy = "operator", cascade = {CascadeType.ALL}, orphanRemoval = true, fetch = FetchType.LAZY)
private List<Product> products = new ArrayList<>();
public void setProducts(List<Product> products) {
this.products = products;
}
}{code}
h3. Test
{noformat} @Test
void shouldDeleteProduct() {
// Given
String string = "ID2";
String operatorID = "operatorID2";
String test = "test";
Operator operator = new Operator(operatorID);
operatorService.addOperator(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
// When
ProductPK productPK = new ProductPK(string, operatorID);
productService.deleteProduct(productPK);
// Then
Optional<Product> byId2 = productService.getProduct(productPK);
assertThat(byId2).isEmpty();
}{noformat}
I get the following exception :
{noformat}org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; statement executed: delete from products where operator_operator_id=? and product_id=? and operator_operator_id is null and product_id is null and description=?
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:307)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:232){noformat}
( https://hibernate.atlassian.net/browse/HHH-16821#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16821#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=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100227- sha1:8ffa416 )
1 year, 3 months
[JIRA] (HHH-16821) Fail to delete enty with a composite id using (an `@IdClass`) with one of its fields mapped from the id of a `@ManyToOne` association`
by Erwan Moutymbo (JIRA)
Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYTQxZmQxMmFh... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiYTQxZm... ) HHH-16821 ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiYTQxZm... ) Fail to delete enty with a composite id using (an `@IdClass`) with one of its fields mapped from the id of a `@ManyToOne` association` ( https://hibernate.atlassian.net/browse/HHH-16821?atlOrigin=eyJpIjoiYTQxZm... )
Issue Type: Bug Affects Versions: 6.2.2, 6.2.3, 6.2.4, 6.2.5 Assignee: Unassigned Components: hibernate-core Created: 19/Jun/2023 05:17 AM Environment: Hibernate: tried 6.2.2.Final to 6.2.5.Final
initially found with Postgresql: 14.5
reproduced with h2 2.1.214
Spring Data JPA: 3.0.6
Spring Boot: 3.0.7
ehcache 3.10.8
JDK: OpenJDK 64-Bit Server VM Temurin-17.0.6
OS: Fedora 38 Priority: Major Reporter: Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... )
Description
-----------
I'm migrating from hibernate 5.6.15 to hibernate 6.2.5 and I have noticed some issues when I try to delete an entity that have a compositeId with one of its id field part of a @ManyToOne association.
Entities
--------
@Getter
@IdClass(ProductPK.class)
@EqualsAndHashCode(onlyExplicitlyIncluded = true )
@ToString(onlyExplicitlyIncluded = true )
@NoArgsConstructor(access = PROTECTED)
@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
@Cacheable
@Cache(usage = READ_WRITE)
@Table(name = "PRODUCTS" )
public class Product {
public Product( String productId, Operator operator ) {
this.productId = productId;
this. operator = operator ;
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "PRODUCT_ID" , nullable = false )
private String productId;
@Id
@EqualsAndHashCode.Include
@ToString.Include
@Getter
@Setter
@Cache(usage = READ_WRITE)
@ManyToOne(fetch = LAZY, optional = false )
@JoinColumn(nullable = false )
private Operator operator ;
@Column(name = "DESCRIPTION" )
@Setter
private String description;
@AllArgsConstructor
@EqualsAndHashCode
@ToString
@NoArgsConstructor(access = PUBLIC)
public static class ProductPK implements Serializable {
private String productId;
private String operator ;
}
}
@Getter
@Entity
@ToString(onlyExplicitlyIncluded = true )
@EqualsAndHashCode(onlyExplicitlyIncluded = true )
@NoArgsConstructor(access = PROTECTED)
@Table(name = "OPERATORS" )
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
@Cacheable
@Cache(usage = READ_WRITE)
public class Operator {
public Operator( String operatorId) {
this.operatorId = operatorId;
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "OPERATOR_ID" , nullable = false )
private String operatorId;
@OneToMany(mappedBy = " operator " , cascade = {CascadeType.ALL}, orphanRemoval = true , fetch = FetchType.LAZY)
private List<Product> products = new ArrayList<>();
public void setProducts(List<Product> products) {
this.products = products;
}
}
Test
----
@Test
void shouldDeleteProduct() {
// Given
String string = "ID2";
String operatorID = "operatorID2";
String test = "test";
Operator operator = new Operator(operatorID);
operatorService.addOperator(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
// When
ProductPK productPK = new ProductPK(string, operatorID);
productService.deleteProduct(productPK);
// Then
Optional<Product> byId2 = productService.getProduct(productPK);
assertThat(byId2).isEmpty();
}
I get the following exception :
org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; statement executed: delete from products where operator_operator_id=? and product_id=? and operator_operator_id is null and product_id is null and description=?
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:307)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:232)
( https://hibernate.atlassian.net/browse/HHH-16821#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16821#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=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100227- sha1:8ffa416 )
1 year, 3 months