-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-19206 Test mutating id verifying it doesn't trigger dirty tracking
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
.../src/test/java/org/hibernate/orm/test/bytecode/enhancement/dirty/DirtyTrackingIdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.bytecode.enhancement.dirty; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import jakarta.persistence.EmbeddedId; | ||
import jakarta.persistence.Entity; | ||
import org.hibernate.testing.bytecode.enhancement.EnhancementOptions; | ||
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Objects; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@JiraKey("HHH-19206") | ||
@DomainModel( | ||
annotatedClasses = { | ||
DirtyTrackingIdTest.MyEntity.class | ||
} | ||
) | ||
@SessionFactory | ||
@BytecodeEnhanced | ||
@EnhancementOptions(lazyLoading = true, inlineDirtyChecking = true, extendedEnhancement = true) | ||
public class DirtyTrackingIdTest { | ||
|
||
@Test | ||
public void test(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
MyEntity myEntity = new MyEntity(); | ||
myEntity.setAnId( new MyEntityId( 1L ) ); | ||
myEntity.setData( "initial" ); | ||
session.persist( myEntity ); | ||
|
||
// This is unnecessary, but should be harmless... | ||
// Unfortunately it causes dirty checking to misbehave. | ||
// Comment it, and the test will pass. | ||
myEntity.setAnId( new MyEntityId( 1L ) ); | ||
|
||
myEntity.setData( "updated" ); | ||
} ); | ||
scope.inTransaction( session -> { | ||
var entityFromDb = session.find( MyEntity.class, new MyEntityId( 1L ) ); | ||
assertThat( entityFromDb.getData() ).isEqualTo( "updated" ); | ||
} ); | ||
} | ||
|
||
// --- // | ||
|
||
@Entity(name = "MyEntity") | ||
public static class MyEntity { | ||
// The name of this property must be (alphabetically) before the name of "data" to trigger the bug. | ||
// Yes, it's weird. | ||
@EmbeddedId | ||
private MyEntityId anId; | ||
private String data; | ||
|
||
public void setAnId(MyEntityId id) { | ||
this.anId = id; | ||
} | ||
|
||
public MyEntityId getAnId() { | ||
return anId; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public void setData(String name) { | ||
this.data = name; | ||
} | ||
} | ||
|
||
@Embeddable | ||
public static class MyEntityId { | ||
private Long id; | ||
|
||
public MyEntityId() { | ||
} | ||
|
||
public MyEntityId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if ( !(o instanceof MyEntityId) ) { | ||
return false; | ||
} | ||
|
||
return Objects.equals( id, ( (MyEntityId) o ).id ); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode( id ); | ||
} | ||
} | ||
} |