Skip to content

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) {
/**
* Resolve an identifier or unique key value
*/
private Object resolve(Object value, SharedSessionContractImplementor session, Object owner) {
protected Object resolve(Object value, SharedSessionContractImplementor session, Object owner) {
if ( value != null && !isNull( owner, session ) ) {
if ( isReferenceToPrimaryKey() ) {
return resolveIdentifier( value, session, null );
Expand Down
24 changes: 15 additions & 9 deletions hibernate-core/src/main/java/org/hibernate/type/OneToOneType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


//the owner of the association is not the owner of the id
Object id = getIdentifierType( session ).assemble( oid, session, null );
Expand All @@ -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
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
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;
}

}