Skip to content
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

HHH-19017: Address ClassCastException for PersistentAttributeInterceptable #9605

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -13,6 +13,7 @@
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.EntityUniqueKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.spi.AppliedGraph;
Expand Down Expand Up @@ -197,28 +198,33 @@ public void resolveInstance(EntityDelayedFetchInitializerData data) {
// because we can't generate a proxy based on the unique key yet
if ( referencedModelPart.isLazy() ) {
instance = UNFETCHED_PROPERTY;
}
else if ( getParent().isEntityInitializer() && isLazyByGraph( rowProcessingState ) ) {
// todo : manage the case when parent is an EmbeddableInitializer
final Object resolvedInstance = getParent().asEntityInitializer()
.getResolvedInstance( rowProcessingState );
final LazyAttributeLoadingInterceptor persistentAttributeInterceptor = (LazyAttributeLoadingInterceptor) ManagedTypeHelper
.asPersistentAttributeInterceptable( resolvedInstance ).$$_hibernate_getInterceptor();

persistentAttributeInterceptor.addLazyFieldByGraph( navigablePath.getLocalName() );
instance = UNFETCHED_PROPERTY;
}
else {
instance = concreteDescriptor.loadByUniqueKey(
uniqueKeyPropertyName,
data.entityIdentifier,
session
);

// If the entity was not in the Persistence Context, but was found now,
// add it to the Persistence Context
if ( instance != null ) {
persistenceContext.addEntity( euk, instance );
} else {
// Try to load a PersistentAttributeInterceptable. If we get one, we can add the lazy
// field to the interceptor. If we don't get one, we load the entity by unique key.
PersistentAttributeInterceptable persistentAttributeInterceptable = null;
if ( getParent().isEntityInitializer() && isLazyByGraph( rowProcessingState ) ) {
final Object resolvedInstance =
getParent().asEntityInitializer().getResolvedInstance( rowProcessingState );
persistentAttributeInterceptable =
ManagedTypeHelper.asPersistentAttributeInterceptableOrNull( resolvedInstance );
}

if ( persistentAttributeInterceptable != null ) {
final LazyAttributeLoadingInterceptor persistentAttributeInterceptor = (LazyAttributeLoadingInterceptor) persistentAttributeInterceptable.$$_hibernate_getInterceptor();
persistentAttributeInterceptor.addLazyFieldByGraph( navigablePath.getLocalName() );
instance = UNFETCHED_PROPERTY;
} else {
instance = concreteDescriptor.loadByUniqueKey(
uniqueKeyPropertyName,
data.entityIdentifier,
session
);

// If the entity was not in the Persistence Context, but was found now,
// add it to the Persistence Context
if ( instance != null ) {
persistenceContext.addEntity( euk, instance );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.lazyonetoone;

import java.util.List;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
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.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hibernate.Hibernate.isInitialized;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DomainModel(
annotatedClasses = {
LazyOneToOneWithEntityGraphTest.Company.class,
LazyOneToOneWithEntityGraphTest.Employee.class,
LazyOneToOneWithEntityGraphTest.Project.class
}
)
@SessionFactory
@BytecodeEnhanced(runNotEnhancedAsWell = true)
public class LazyOneToOneWithEntityGraphTest {
@BeforeAll
void setUp(SessionFactoryScope scope) {
scope.inTransaction(session -> {
// Create company
Company company = new Company();
company.id = 1L;
company.name = "Hibernate";
session.persist(company);

// Create project
Project project = new Project();
project.id = 1L;
session.persist(project);

// Create employee
Employee employee = new Employee();
employee.id = 1L;
employee.company = company;
employee.projects = List.of(project);
session.persist(employee);
});
}

@AfterAll
void tearDown(SessionFactoryScope scope) {
scope.inTransaction(session -> {
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
});
}


@Test
void reproducerTest(SessionFactoryScope scope) {
scope.inTransaction(session -> {
// Load employee using entity graph
Employee employee = session.createQuery(
"select e from Employee e where e.id = :id", Employee.class)
.setParameter("id", 1L)
.setHint("javax.persistence.fetchgraph", session.getEntityGraph("employee.projects"))
.getSingleResult();

assertTrue(isInitialized(employee.projects));
assertEquals("Hibernate", employee.company.name);
});
}

@Entity(name = "Company")
public static class Company {
@Id
private Long id;

private String name;
}

@Entity(name = "Employee")
@NamedEntityGraph(
name = "employee.projects",
attributeNodes = @NamedAttributeNode("projects")
)
public static class Employee {
@Id
private Long id;

@OneToOne
@JoinColumn(name = "company_name", referencedColumnName = "name")
private Company company;

@OneToMany(fetch = FetchType.LAZY)
private List<Project> projects;
}

@Entity(name = "Project")
public static class Project {
@Id
private Long id;
}
}