From 633deeb75e1a0d7ff4f08a27ffc5360d2605fecf Mon Sep 17 00:00:00 2001 From: Oliver Breidenbach Date: Mon, 13 Jun 2016 11:30:18 +0200 Subject: [PATCH] HHH-10842 - Entity graph attribute node is ignored if the entity is mapped by the Primary Key column - Add test case to replicate the issue --- .../mapped_by_id/LoadGraphFindByIdTest.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/graphs/mapped_by_id/LoadGraphFindByIdTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/graphs/mapped_by_id/LoadGraphFindByIdTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/graphs/mapped_by_id/LoadGraphFindByIdTest.java new file mode 100644 index 0000000000..77c47c6635 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/graphs/mapped_by_id/LoadGraphFindByIdTest.java @@ -0,0 +1,127 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpa.test.graphs.mapped_by_id; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.Entity; +import javax.persistence.EntityGraph; +import javax.persistence.EntityManager; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Oliver Breidenbach + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class LoadGraphFindByIdTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {User.class, UserStatistic.class}; + } + + @Override + protected void afterEntityManagerFactoryBuilt() { + doInJPA( this::entityManagerFactory, em -> { + UserStatistic statistic = new UserStatistic(); + statistic.id = 1L; + statistic.commentCount = 7; + User user = new User(); + user.id = 1L; + user.userStatistic = statistic; + + em.persist( statistic ); + em.persist( user ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-10842") + @FailureExpected( jiraKey = "HHH-10842" ) + public void findByPrimaryKeyWithId() { + doInJPA( this::entityManagerFactory, em -> { + User result = em.find( User.class, 1L, createProperties( em ) ); + Assert.assertNotNull( result.userStatistic.commentCount ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-10842") + public void findByPrimaryKeyWithQuery() { + doInJPA( this::entityManagerFactory, em -> { + User result = createTypedQuery( em ).getSingleResult(); + Assert.assertNotNull( result.userStatistic.commentCount ); + } ); + } + + private TypedQuery createTypedQuery(EntityManager em) { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( User.class ); + Root root = cq.from( User.class ); + + cq.where( cb.equal( root.get( "id" ), 1L ) ); + TypedQuery tq = em.createQuery( cq ); + tq.setHint( "javax.persistence.loadgraph", createEntityGraph( em ) ); + return tq; + } + + private Map createProperties(EntityManager em) { + Map properties = new HashMap(); + properties.put( + "javax.persistence.loadgraph", + createEntityGraph( em ) + ); + return properties; + } + + private EntityGraph createEntityGraph(EntityManager em) { + EntityGraph entityGraph = em.createEntityGraph( User.class ); + entityGraph.addAttributeNodes( "userStatistic" ); + return entityGraph; + } + + @Entity(name = "UserStatistic") + public static class UserStatistic { + + @Id + private Long id; + + private Integer commentCount; + } + + @Entity(name = "User") + public static class User { + + @Id + private Long id; + + private String name; + + @OneToOne(fetch = FetchType.LAZY, optional = false) + @MapsId + private UserStatistic userStatistic; + + } +}