HHH-15859 Fetching an entity with entity graph for an attribute of type Map fails with an assertion error

This commit is contained in:
rgarcia 2022-12-14 16:48:08 +01:00 committed by Christian Beikov
parent c67b3f984a
commit de68924ad2
2 changed files with 54 additions and 10 deletions

View File

@ -23,8 +23,6 @@ import org.hibernate.sql.results.graph.EntityGraphTraversalState;
import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.graph.Fetchable; import org.hibernate.sql.results.graph.Fetchable;
import jakarta.persistence.metamodel.PluralAttribute;
/** /**
* @author Nathan Xu * @author Nathan Xu
*/ */
@ -69,9 +67,6 @@ public class StandardEntityGraphTraversalStateImpl implements EntityGraphTravers
if ( fetchable instanceof PluralAttributeMapping ) { if ( fetchable instanceof PluralAttributeMapping ) {
PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable; PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
assert exploreKeySubgraph && isJpaMapCollectionType( pluralAttributeMapping )
|| !exploreKeySubgraph && !isJpaMapCollectionType( pluralAttributeMapping );
if ( exploreKeySubgraph ) { if ( exploreKeySubgraph ) {
subgraphMap = attributeNode.getKeySubGraphMap(); subgraphMap = attributeNode.getKeySubGraphMap();
subgraphMapKey = getEntityCollectionPartJavaClass( pluralAttributeMapping.getIndexDescriptor() ); subgraphMapKey = getEntityCollectionPartJavaClass( pluralAttributeMapping.getIndexDescriptor() );
@ -120,8 +115,4 @@ public class StandardEntityGraphTraversalStateImpl implements EntityGraphTravers
return fetchParent.appliesTo( currentGraphContext ); return fetchParent.appliesTo( currentGraphContext );
} }
private static boolean isJpaMapCollectionType(PluralAttributeMapping pluralAttributeMapping) {
return pluralAttributeMapping.getCollectionDescriptor().getCollectionSemantics().getCollectionClassification().toJpaClassification() == PluralAttribute.CollectionType.MAP;
}
} }

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.orm.test.jpa.graphs; package org.hibernate.orm.test.jpa.graphs;
import jakarta.persistence.MapKey;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@ -44,7 +45,7 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase {
@Override @Override
protected Class<?>[] getAnnotatedClasses() { 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 }; Company.class, Employee.class, Manager.class, Location.class };
} }
@ -297,6 +298,36 @@ public class EntityGraphTest extends BaseEntityManagerFunctionalTestCase {
em.close(); 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<Author> entityGraph = em.createEntityGraph(Author.class);
entityGraph.addAttributeNodes("books");
Map<String, Object> 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 @Entity
@Table(name = "foo") @Table(name = "foo")
public static class 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<Integer, Book> books = new HashMap<>();
}
} }