[JIRA] (HHH-16811) Dirty property lost and not detected with Batch Fetch, Embedded and FetchMode SELECT
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNzhjZWQ2YmE0... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16811?atlOrigin=eyJpIjoiNzhjZW... ) HHH-16811 ( https://hibernate.atlassian.net/browse/HHH-16811?atlOrigin=eyJpIjoiNzhjZW... ) Dirty property lost and not detected with Batch Fetch, Embedded and FetchMode SELECT ( https://hibernate.atlassian.net/browse/HHH-16811?atlOrigin=eyJpIjoiNzhjZW... )
Issue Type: Bug Affects Versions: 6.2.5 Assignee: Unassigned Components: hibernate-core Created: 16/Jun/2023 09:15 AM Priority: Critical Reporter: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
The dirty property is lost (reset) in BatchEntityInsideEmbeddableSelectFetchInitializer. updateRootEntityLoadedState (maybe the same could happen in BatchEntitySelectFetchInitializer. updateParentEntityLoadedState ) for the following scenario:
// initial investigation started with dynamic update since there were other dirty properties
// but this minimal test case also fails without dynamic update since myEmbeddable is the only dirty property in this test case
@org.hibernate.annotations.DynamicUpdate
@Entity
@Table(name = "ENTITY_A" )
public class EntityA {
@Id
@Column(name = "ID" )
Integer id;
@Embedded
MyEmbeddableComponent myEmbeddable;
@JoinColumn(name = "ENTITY_B" )
@ManyToOne
EntityB entityB;
}
@Entity
@Table(name = "ENTITY_B" )
public class EntityB {
@Id
@Column(name = "ID" )
Integer id;
@OneToMany(mappedBy = "entityB" )
final List<EntityA> listOfEntityA = new ArrayList<>();
}
@Entity
@Table(name = "ENTITY_C" )
public class EntityC {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID" )
Integer id;
}
@Embeddable
public class MyEmbeddableComponent {
@Column(name = "MY_EMB_REF" )
String embRef;
@ManyToOne
@JoinColumn(name = "ENTITY_C" )
@Fetch(FetchMode.SELECT)
EntityC entityC;
}
@Override
protected void configure(Configuration configuration) {
super.configure(configuration);
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "2" );
}
@Test
public void hhhXXXTest() throws Exception {
Integer idA = 1;
Integer idB = 2;
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
MyEmbeddableComponent embeddable = new MyEmbeddableComponent();
embeddable.embRef = "some-ref" ;
EntityA entityA = new EntityA();
EntityB entityB = new EntityB();
EntityC entityC = new EntityC();
embeddable.entityC = entityC;
entityA.id = idA;
entityA.myEmbeddable = embeddable;
entityA.entityB = entityB;
entityB.id = idB;
entityB.listOfEntityA.add(entityA);
s.persist(entityC);
s.persist(entityB);
s.persist(entityA);
tx.commit();
}
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
EntityA entityA = s.find(EntityA.class, idA);
entityA.myEmbeddable = null ;
EntityB entityB = s.find(EntityB.class, idB);
// the following lazy loading will initialize the collection and it will reset
// the entityA state, losing the dirtied myEmbeddable property value
assertThat(entityB.listOfEntityA).hasSize(1);
tx.commit();
}
try (Session s = openSession()) {
EntityA entityA = s.find(EntityA.class, idA);
assertThat(entityA.myEmbeddable).isNull();
}
}
org.opentest4j.AssertionFailedError:
Expecting:
<org.hibernate.bugs.MyEmbeddableComponent@65d0a110>
to be equal to:
<null>
but was not.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at org.hibernate.bugs.ORMUnitTestCase.hhhXXXTest(ORMUnitTestCase.java:100)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833)
This used to work just fine with latest 5.6 hibernate series.
The failing test case will be attached
( https://hibernate.atlassian.net/browse/HHH-16811#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16811#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:5147d46 )
1 year, 3 months
[JIRA] (HHH-16810) Fail to delete entity that contains an embedded with a many to many association as field
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=eyJpIjoiMmU1OTFlODBj... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16810?atlOrigin=eyJpIjoiMmU1OT... ) HHH-16810 ( https://hibernate.atlassian.net/browse/HHH-16810?atlOrigin=eyJpIjoiMmU1OT... ) Fail to delete entity that contains an embedded with a many to many association as field ( https://hibernate.atlassian.net/browse/HHH-16810?atlOrigin=eyJpIjoiMmU1OT... )
Change By: Erwan Moutymbo ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=640210c... )
h5. 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 contains an embedded which contain itself a field describing a many to many association.
h3. Entities
{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;
this.tagPolicy = new OperatorTagPolicy(Set.of(), ALL);
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "OPERATOR_ID", nullable = false)
private String operatorId;
@Embedded
private OperatorTagPolicy tagPolicy;
@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;
}
@Value
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
@Embeddable
public static class OperatorTagPolicy {
@NonFinal
@ManyToMany(fetch = LAZY)
@JoinTable(
name = "OPERATOR_TO_TAG",
joinColumns = {
@JoinColumn(name = "OPERATOR_ID", referencedColumnName = "OPERATOR_ID")
},
inverseJoinColumns = @JoinColumn(name = "TAG_NAME"))
@Cache(usage = READ_WRITE)
Set<Tag> exceptions;
@NonFinal
@Column(name = "TAG_POLICY_TYPE", nullable = false)
@Enumerated(EnumType.STRING)
PolicyType type;
}
}{code}
{code:java}@Entity
@Table(name = "TAGS")
@EqualsAndHashCode
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor
@Getter
public class Tag {
@Id
@Column(name = "TAG_NAME", nullable = false)
private String name;
}{code}
{code:java}public enum PolicyType {
ALL,
NONE
}{code}
h3. Tests
{ noformat code:java } @Test
@Order(2)
void shouldDeleteOperator() {
String string = "ID2";
String operatorID = "operatorID2";
String test = "test";
Operator operator = new Operator(operatorID);
operatorDao.save(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
operatorService.deleteOperator(operatorID);
Optional<Operator> byId2 = operatorService.getOperator(operatorID);
assertThat(byId2).isEmpty();
}
@Test
@Order(3)
void shouldDeleteAllOperators() {
String string = "ID3";
String operatorID = "operatorID3";
String test = "test";
Operator operator = new Operator(operatorID);
operatorDao.save(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
operatorService.deleteAllOperators();
Optional<Operator> byId2 = operatorService.getOperator(operatorID);
assertThat(byId2).isEmpty();
}{ noformat code }
These tests raise an exception when the delete (or deleteAll) is committed.
{noformat}org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:570)
...
Caused by: jakarta.persistence.RollbackException: Error while committing the transaction
...
Caused by: java.lang.UnsupportedOperationException
at org.hibernate.metamodel.mapping.internal.PluralAttributeMappingImpl.breakDownJdbcValues(PluralAttributeMappingImpl.java:975)
at org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.breakDownJdbcValues(EmbeddableMappingTypeImpl.java:655)
...{noformat}
GitHub repository with sources : [https://github.com/emouty/hibernate-L2-cache-issue|https://github.com/emo...]
( https://hibernate.atlassian.net/browse/HHH-16810#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16810#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:5147d46 )
1 year, 3 months
[JIRA] (HHH-16810) Fail to delete all enti
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=eyJpIjoiZTM0OTQ5Yzgx... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16810?atlOrigin=eyJpIjoiZTM0OT... ) HHH-16810 ( https://hibernate.atlassian.net/browse/HHH-16810?atlOrigin=eyJpIjoiZTM0OT... ) Fail to delete all enti ( https://hibernate.atlassian.net/browse/HHH-16810?atlOrigin=eyJpIjoiZTM0OT... )
Issue Type: Bug Affects Versions: 6.2.0, 6.2.1, 6.2.2, 6.2.3, 6.2.4, 6.2.5 Assignee: Unassigned Components: hibernate-core Created: 16/Jun/2023 09:10 AM Environment: Hibernate: tried 6.2.0.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 contains an embedded which contain itself a field describing a many to many association.
Entities
--------
@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;
this.tagPolicy = new OperatorTagPolicy(Set.of(), ALL);
}
@EqualsAndHashCode.Include
@ToString.Include
@Id
@Column(name = "OPERATOR_ID" , nullable = false )
private String operatorId;
@Embedded
private OperatorTagPolicy tagPolicy;
@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;
}
@Value
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
@Embeddable
public static class OperatorTagPolicy {
@NonFinal
@ManyToMany(fetch = LAZY)
@JoinTable(
name = "OPERATOR_TO_TAG" ,
joinColumns = {
@JoinColumn(name = "OPERATOR_ID" , referencedColumnName = "OPERATOR_ID" )
},
inverseJoinColumns = @JoinColumn(name = "TAG_NAME" ))
@Cache(usage = READ_WRITE)
Set<Tag> exceptions;
@NonFinal
@Column(name = "TAG_POLICY_TYPE" , nullable = false )
@Enumerated(EnumType.STRING)
PolicyType type;
}
}
@Entity
@Table(name = "TAGS" )
@EqualsAndHashCode
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor
@Getter
public class Tag {
@Id
@Column(name = "TAG_NAME" , nullable = false )
private String name;
}
public enum PolicyType {
ALL,
NONE
}
Tests
-----
@Test
@Order(2)
void shouldDeleteOperator() {
String string = "ID2";
String operatorID = "operatorID2";
String test = "test";
Operator operator = new Operator(operatorID);
operatorDao.save(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
operatorService.deleteOperator(operatorID);
Optional<Operator> byId2 = operatorService.getOperator(operatorID);
assertThat(byId2).isEmpty();
}
@Test
@Order(3)
void shouldDeleteAllOperators() {
String string = "ID3";
String operatorID = "operatorID3";
String test = "test";
Operator operator = new Operator(operatorID);
operatorDao.save(operator);
Product product = new Product(string, operator);
product.setDescription(test);
productService.addProduct(product);
operatorService.deleteAllOperators();
Optional<Operator> byId2 = operatorService.getOperator(operatorID);
assertThat(byId2).isEmpty();
}
These tests raise an exception when the delete (or deleteAll) is committed.
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:570)
...
Caused by: jakarta.persistence.RollbackException: Error while committing the transaction
...
Caused by: java.lang.UnsupportedOperationException
at org.hibernate.metamodel.mapping.internal.PluralAttributeMappingImpl.breakDownJdbcValues(PluralAttributeMappingImpl.java:975)
at org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.breakDownJdbcValues(EmbeddableMappingTypeImpl.java:655)
...
GitHub repository with sources : https://github.com/emouty/hibernate-L2-cache-issue
( https://hibernate.atlassian.net/browse/HHH-16810#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16810#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:5147d46 )
1 year, 3 months