Skip to content
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 @@ -1605,7 +1605,9 @@ private Object initLazyProperties(

final var interceptor = asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
assert interceptor != null : "Expecting bytecode interceptor to be non-null";
final Set<String> initializedLazyAttributeNames = interceptor.getInitializedLazyAttributeNames();
// Create a copy of init attrs, since lazySelectLoadPlan.load may update the set inside the interceptor,
// and we end up with the modified one here:
final Set<String> initializedLazyAttributeNames = new HashSet<>( interceptor.getInitializedLazyAttributeNames() );
Comment on lines -1608 to +1610
Copy link
Member

Choose a reason for hiding this comment

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

I think this might be only "hiding" the problem: we don't want this method to ignore the lazy attribute interceptor's initialized attributes set, as they might be read by other logic (e.g. field writes) instead of explicitly by this method.

I tried looking into this, and the lazySelectLoadPlan.load() call seems to be doing just that, so I believe a better fix would be adapting the for loop that iterates through the lazy-group attributes to account for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍🏻 if you'll be looking into this, let's push that set variable initialization waaaaaaaaaaaaaay down after the lazySelectLoadPlan.load. Because as the things are atm it's confusing at best, that the initializedLazyAttributeNames changes mid method 🙂


final var lazyAttributesMetadata = getBytecodeEnhancementMetadata().getLazyAttributesMetadata();
final String fetchGroup = lazyAttributesMetadata.getFetchGroupName( fieldName );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.mapping.lazytoone;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import org.hibernate.Hibernate;
import org.hibernate.annotations.LazyGroup;
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.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@DomainModel(
annotatedClasses = {
LazyOneToOneWithCastTest.ContainingEntity.class,
LazyOneToOneWithCastTest.ContainedEntity.class,
}
)
@SessionFactory
@BytecodeEnhanced
@EnhancementOptions(lazyLoading = true)
class LazyOneToOneWithCastTest {

@Test
void oneNullOneNotNull(SessionFactoryScope scope) {
scope.inTransaction( session -> {
ContainingEntity containingEntity1 = new ContainingEntity();
containingEntity1.setId( 2 );

ContainedEntity contained1 = new ContainedEntity();
contained1.setId( 4 );
contained1.setContainingAsIndexedEmbeddedWithCast( containingEntity1 );
containingEntity1.setContainedIndexedEmbeddedWithCast( contained1 );

session.persist( contained1 );
session.persist( containingEntity1 );

} );

scope.inTransaction( session -> {
ContainedEntity contained = session.find( ContainedEntity.class, 4 );

ContainingEntity containingAsIndexedEmbedded = contained.getContainingAsIndexedEmbedded();
assertThat( containingAsIndexedEmbedded ).isNull();
assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbedded" ) ).isTrue();
assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isFalse();

Object containingAsIndexedEmbeddedWithCast = contained.getContainingAsIndexedEmbeddedWithCast();
assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isTrue();
assertThat( containingAsIndexedEmbeddedWithCast ).isNotNull();
} );

scope.inTransaction( session -> {
ContainedEntity contained = session.find( ContainedEntity.class, 4 );
assertThat( contained.getContainingAsIndexedEmbeddedWithCast() ).isNotNull();
} );
}

@Test
void bothNotNull(SessionFactoryScope scope) {
scope.inTransaction( session -> {
ContainingEntity containingEntity1 = new ContainingEntity();
containingEntity1.setId( 1 );

ContainedEntity contained1 = new ContainedEntity();
contained1.setId( 4 );
contained1.setContainingAsIndexedEmbeddedWithCast( containingEntity1 );
containingEntity1.setContainedIndexedEmbeddedWithCast( contained1 );

contained1.setContainingAsIndexedEmbedded( containingEntity1 );
containingEntity1.setContainedIndexedEmbedded( contained1 );


session.persist( contained1 );
session.persist( containingEntity1 );

} );

scope.inTransaction( session -> {
ContainedEntity contained = session.find( ContainedEntity.class, 4 );

ContainingEntity containingAsIndexedEmbedded = contained.getContainingAsIndexedEmbedded();
assertThat( containingAsIndexedEmbedded ).isNotNull();
assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbedded" ) ).isTrue();
assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isFalse();

Object containingAsIndexedEmbeddedWithCast = contained.getContainingAsIndexedEmbeddedWithCast();
assertThat( Hibernate.isPropertyInitialized( contained, "containingAsIndexedEmbeddedWithCast" ) ).isTrue();
assertThat( containingAsIndexedEmbeddedWithCast ).isNotNull();
} );

scope.inTransaction( session -> {
ContainedEntity contained = session.find( ContainedEntity.class, 4 );
assertThat( contained.getContainingAsIndexedEmbeddedWithCast() ).isNotNull();
} );
}

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
}

@Entity(name = "containing")
public static class ContainingEntity {

@Id
private Integer id;

@OneToOne
private ContainedEntity containedIndexedEmbedded;

@OneToOne(targetEntity = ContainedEntity.class)
@JoinColumn(name = "CIndexedEmbeddedCast")
private ContainedEntity containedIndexedEmbeddedWithCast;


public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public ContainedEntity getContainedIndexedEmbedded() {
return containedIndexedEmbedded;
}

public Object getContainedIndexedEmbeddedWithCast() {
return containedIndexedEmbeddedWithCast;
}

public void setContainedIndexedEmbeddedWithCast(ContainedEntity containedIndexedEmbeddedWithCast) {
this.containedIndexedEmbeddedWithCast = containedIndexedEmbeddedWithCast;
}

public void setContainedIndexedEmbedded(ContainedEntity containedIndexedEmbedded) {
this.containedIndexedEmbedded = containedIndexedEmbedded;
}
}

@Entity(name = "contained")
public static class ContainedEntity {
@Id
private Integer id;

@OneToOne(mappedBy = "containedIndexedEmbeddedWithCast", targetEntity = ContainingEntity.class,
fetch = FetchType.LAZY)
@LazyGroup("containingAsIndexedEmbeddedWithCast")
private ContainingEntity containingAsIndexedEmbeddedWithCast;

@OneToOne(mappedBy = "containedIndexedEmbedded", fetch = FetchType.LAZY)
@LazyGroup("containingAsIndexedEmbedded")
private ContainingEntity containingAsIndexedEmbedded;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public ContainingEntity getContainingAsIndexedEmbedded() {
return containingAsIndexedEmbedded;
}

public Object getContainingAsIndexedEmbeddedWithCast() {
return containingAsIndexedEmbeddedWithCast;
}

public void setContainingAsIndexedEmbeddedWithCast(ContainingEntity containingAsIndexedEmbeddedWithCast) {
this.containingAsIndexedEmbeddedWithCast = containingAsIndexedEmbeddedWithCast;
}

public void setContainingAsIndexedEmbedded(ContainingEntity containingAsIndexedEmbedded) {
this.containingAsIndexedEmbedded = containingAsIndexedEmbedded;
}
}
}
Loading