-
Notifications
You must be signed in to change notification settings - Fork 3.6k
HHH-15045 avoid dirty-flush for OneToOne type #5061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -106,17 +106,12 @@ public boolean isOneToOne() { | |
|
||
@Override | ||
public boolean isDirty(Object old, Object current, SharedSessionContractImplementor session) { | ||
if ( isSame( old, current ) ) { | ||
return false; | ||
} | ||
|
||
return getIdentifierType( session ) | ||
.isDirty( getIdentifier( old, session ), getIdentifier( current, session ), session ); | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isDirty(Object old, Object current, boolean[] checkable, SharedSessionContractImplementor session) { | ||
return isDirty(old, current, session); | ||
return false; | ||
} | ||
|
||
@Override | ||
|
@@ -159,6 +154,12 @@ public Serializable disassemble(Object value, SharedSessionContractImplementor s | |
|
||
@Override | ||
public Object assemble(Serializable oid, SharedSessionContractImplementor session, Object owner) throws HibernateException { | ||
if ( oid == null ) { | ||
if ( uniqueKeyPropertyName != null ) { | ||
return resolve( session.getContextEntityIdentifier( owner ), session, owner ); | ||
} | ||
return null; | ||
} | ||
|
||
//the owner of the association is not the owner of the id | ||
Object id = getIdentifierType( session ).assemble( oid, session, null ); | ||
|
@@ -169,9 +170,14 @@ public Object assemble(Serializable oid, SharedSessionContractImplementor sessio | |
|
||
return resolveIdentifier( id, session ); | ||
} | ||
|
||
|
||
/** | ||
* We don't need to dirty check one-to-one because of how | ||
* assemble/disassemble is implemented and because a one-to-one | ||
* association is never dirty | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reverted original comment says all |
||
@Override | ||
public boolean isAlwaysDirtyChecked() { | ||
return true; | ||
return false; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/flush/DirtyFlushTest.java
This file contains hidden or 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,100 @@ | ||
package org.hibernate.orm.test.onetoone.flush; | ||
|
||
import org.hibernate.cfg.AvailableSettings; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
import org.hibernate.testing.orm.junit.Jpa; | ||
import org.hibernate.testing.orm.junit.Setting; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.OneToOne; | ||
import jakarta.persistence.Version; | ||
|
||
/** | ||
* @author Nathan Xu | ||
*/ | ||
@TestForIssue( jiraKey = "HHH-15045" ) | ||
@Jpa( | ||
annotatedClasses = { | ||
DirtyFlushTest.User.class, | ||
DirtyFlushTest.Profile.class | ||
}, | ||
properties = @Setting(name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true") | ||
) | ||
class DirtyFlushTest { | ||
|
||
@BeforeEach | ||
void setUp(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( em -> { | ||
final User user = new User(); | ||
user.id = 1; | ||
user.version = 1; | ||
|
||
final Profile profile = new Profile(); | ||
profile.id = 1; | ||
profile.version = 1; | ||
|
||
em.persist( user ); | ||
em.persist( profile ); | ||
} ); | ||
} | ||
|
||
@Test | ||
void testDirtyFlushNotHappened(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( em -> { | ||
final User user = em.find( User.class, 1 ); | ||
final Profile profile = em.find( Profile.class, 1 ); | ||
profile.user = user; | ||
user.profile = profile; | ||
|
||
em.persist( profile ); | ||
em.flush(); | ||
} ); | ||
|
||
scope.inTransaction( em -> { | ||
final Profile profile = em.find( Profile.class, 1 ); | ||
Assertions.assertEquals( 2, profile.version ); | ||
|
||
final User user = em.find( User.class, 1 ); | ||
Assertions.assertEquals( 1, user.version, "without fixing, the version will be bumped due to erroneous dirty flushing" ); | ||
} ); | ||
} | ||
|
||
@AfterEach | ||
void tearDown(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( em -> { | ||
em.createQuery( "delete from Profile" ).executeUpdate(); | ||
em.createQuery( "delete from User" ).executeUpdate(); | ||
} ); | ||
} | ||
|
||
|
||
@Entity(name = "User") | ||
static class User { | ||
@Id | ||
int id; | ||
|
||
@Version | ||
int version; | ||
|
||
@OneToOne(mappedBy = "user") | ||
Profile profile; | ||
} | ||
|
||
@Entity(name = "Profile") | ||
static class Profile { | ||
@Id | ||
int id; | ||
|
||
@Version int version; | ||
@OneToOne // internally Hibernate will use `@ManyToOne` for this field | ||
User user; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dreab8 , I saw there were quite some back and forth (see https://hibernate.atlassian.net/browse/HHH-15044); but I think after we revert back the dirty flush related code changes, the above code seems to become necessary again. I am not sure about this, though. Feel free to take over and work on top of my PR or even close it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NathanQingyangXu this is the discussion that led to the revert https://hibernate.zulipchat.com/#narrow/stream/132094-hibernate-orm-dev/topic/PR.204221