diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java index 51b275e5f6..875bffd917 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/StandardEntityGraphTraversalStateImpl.java @@ -23,8 +23,6 @@ import org.hibernate.sql.results.graph.EntityGraphTraversalState; import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.Fetchable; -import jakarta.persistence.metamodel.PluralAttribute; - /** * @author Nathan Xu */ @@ -69,9 +67,6 @@ public class StandardEntityGraphTraversalStateImpl implements EntityGraphTravers if ( fetchable instanceof PluralAttributeMapping ) { PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable; - assert exploreKeySubgraph && isJpaMapCollectionType( pluralAttributeMapping ) - || !exploreKeySubgraph && !isJpaMapCollectionType( pluralAttributeMapping ); - if ( exploreKeySubgraph ) { subgraphMap = attributeNode.getKeySubGraphMap(); subgraphMapKey = getEntityCollectionPartJavaClass( pluralAttributeMapping.getIndexDescriptor() ); @@ -120,8 +115,4 @@ public class StandardEntityGraphTraversalStateImpl implements EntityGraphTravers return fetchParent.appliesTo( currentGraphContext ); } - private static boolean isJpaMapCollectionType(PluralAttributeMapping pluralAttributeMapping) { - return pluralAttributeMapping.getCollectionDescriptor().getCollectionSemantics().getCollectionClassification().toJpaClassification() == PluralAttribute.CollectionType.MAP; - } - } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/EntityGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/EntityGraphTest.java index af12d6bf0d..5b06f8618b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/EntityGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/EntityGraphTest.java @@ -6,6 +6,7 @@ */ package org.hibernate.orm.test.jpa.graphs; +import jakarta.persistence.MapKey; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -44,7 +45,7 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase { @Override protected Class[] getAnnotatedClasses() { - return new Class[] { Foo.class, Bar.class, Baz.class, + return new Class[] { Foo.class, Bar.class, Baz.class, Author.class, Book.class, Company.class, Employee.class, Manager.class, Location.class }; } @@ -297,6 +298,36 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase { em.close(); } + @Test + @TestForIssue(jiraKey = "HHH-15859") + public void mapAttributeTest() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + Author author = new Author(); + em.persist(author); + + Book book = new Book(); + author.books.put(1, book); + em.persist(author); + + em.getTransaction().commit(); + em.clear(); + + em.getTransaction().begin(); + EntityGraph entityGraph = em.createEntityGraph(Author.class); + entityGraph.addAttributeNodes("books"); + Map properties = new HashMap<>(); + properties.put("javax.persistence.loadgraph", entityGraph); + + Author result = em.find(Author.class, author.id, properties); + assertTrue(Hibernate.isInitialized(result)); + assertTrue(Hibernate.isInitialized(result.books)); + + em.getTransaction().commit(); + em.close(); + } + @Entity @Table(name = "foo") public static class Foo { @@ -337,4 +368,26 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase { } + @Entity + @Table(name = "book") + public static class Book { + @Id + @GeneratedValue + public Integer id; + + @ManyToOne(fetch = FetchType.LAZY) + private Author author; + } + + @Entity + @Table(name = "author") + public static class Author { + @Id + @GeneratedValue + public Integer id; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "author") + @MapKey + public Map books = new HashMap<>(); + } }