HHH-12871 - Fix test that uses dynamic-map entities which failed.

This commit is contained in:
Chris Cranford 2018-08-07 09:30:38 -04:00
parent b9e0449602
commit 8bd79b29cf
1 changed files with 96 additions and 85 deletions

View File

@ -1,85 +1,96 @@
/* /*
* Hibernate, Relational Persistence for Idiomatic Java * Hibernate, Relational Persistence for Idiomatic Java
* *
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.jpa.test.criteria.paths; package org.hibernate.jpa.test.criteria.paths;
import javax.persistence.EntityManager; import java.util.Map;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.TypedQuery;
import javax.persistence.criteria.From; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.JoinType; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path; import javax.persistence.criteria.From;
import javax.persistence.metamodel.Attribute; import javax.persistence.criteria.JoinType;
import javax.persistence.metamodel.Bindable; import javax.persistence.criteria.Path;
import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Type; import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.SingularAttribute;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import javax.persistence.metamodel.Type;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.PathSource; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.query.criteria.internal.path.SingularAttributeJoin; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.junit.Test; import org.hibernate.query.criteria.internal.PathSource;
import org.hibernate.query.criteria.internal.path.SingularAttributeJoin;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import org.junit.Test;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
/** import static org.mockito.Mockito.when;
* @author Brad Koehn import static org.mockito.Mockito.withSettings;
*/
public class SingularAttributeJoinTest extends BaseEntityManagerFunctionalTestCase { /**
@Override * @author Brad Koehn
protected String[] getMappings() { */
return new String[] { public class SingularAttributeJoinTest extends BaseEntityManagerFunctionalTestCase {
getClass().getPackage().getName().replace( '.', '/' ) + "/PolicyAndDistribution.hbm.xml" @Override
}; protected String[] getMappings() {
} return new String[] {
getClass().getPackage().getName().replace( '.', '/' ) + "/PolicyAndDistribution.hbm.xml"
/** };
* When building a join from a non-class based entity (EntityMode.MAP), make sure you get the Bindable from }
* the SingularAttribute as the join model. If you don't, you'll get the first non-classed based entity
* you added to your configuration. Regression for HHH-9142. @Override
*/ protected void addConfigOptions(Map options) {
@Test super.addConfigOptions( options );
public void testEntityModeMapJoins() throws Exception {
CriteriaBuilderImpl criteriaBuilder = mock( CriteriaBuilderImpl.class); // make sure that dynamic-map mode entity types are returned in the metamodel.
PathSource pathSource = mock( PathSource.class); options.put( AvailableSettings.JPA_METAMODEL_POPULATION, "enabled" );
SingularAttribute joinAttribute = mock( SingularAttribute.class); }
when(joinAttribute.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.MANY_TO_ONE);
Type joinType = mock( Type.class, withSettings().extraInterfaces( Bindable.class)); /**
when(joinAttribute.getType()).thenReturn(joinType); * When building a join from a non-class based entity (EntityMode.MAP), make sure you get the Bindable from
SingularAttributeJoin join = new SingularAttributeJoin(criteriaBuilder, null, pathSource, joinAttribute, JoinType.LEFT); * the SingularAttribute as the join model. If you don't, you'll get the first non-classed based entity
* you added to your configuration. Regression for HHH-9142.
assertEquals( joinType, join.getModel()); */
} @Test
public void testEntityModeMapJoins() throws Exception {
@Test CriteriaBuilderImpl criteriaBuilder = mock( CriteriaBuilderImpl.class);
public void testEntityModeMapJoinCriteriaQuery() throws Exception { PathSource pathSource = mock( PathSource.class);
final EntityManager entityManager = entityManagerFactory().createEntityManager(); SingularAttribute joinAttribute = mock( SingularAttribute.class);
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); when(joinAttribute.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.MANY_TO_ONE);
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(); Type joinType = mock( Type.class, withSettings().extraInterfaces( Bindable.class));
javax.persistence.metamodel.EntityType distributionEntity = getEntityType("Distribution"); when(joinAttribute.getType()).thenReturn(joinType);
From distributionFrom = criteriaQuery.from(distributionEntity); SingularAttributeJoin join = new SingularAttributeJoin(criteriaBuilder, null, pathSource, joinAttribute, JoinType.LEFT);
From policyJoin = distributionFrom.join("policy");
Path policyId = policyJoin.get("policyId"); assertEquals( joinType, join.getModel());
criteriaQuery.select(policyId); }
TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
// typedQuery.getResultList(); @Test
} public void testEntityModeMapJoinCriteriaQuery() throws Exception {
final EntityManager entityManager = entityManagerFactory().createEntityManager();
private javax.persistence.metamodel.EntityType getEntityType(String entityName) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
for(javax.persistence.metamodel.EntityType entityType : entityManagerFactory().getMetamodel().getEntities()) { CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
if (entityType.getName().equals("Distribution")) { javax.persistence.metamodel.EntityType distributionEntity = getEntityType("Distribution");
return entityType; From distributionFrom = criteriaQuery.from(distributionEntity);
} From policyJoin = distributionFrom.join("policy");
} Path policyId = policyJoin.get("policyId");
criteriaQuery.select(policyId);
throw new IllegalStateException("Unable to find entity " + entityName); TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
} // typedQuery.getResultList();
} }
private javax.persistence.metamodel.EntityType getEntityType(String entityName) {
for(javax.persistence.metamodel.EntityType entityType : entityManagerFactory().getMetamodel().getEntities()) {
if (entityType.getName().equals("Distribution")) {
return entityType;
}
}
throw new IllegalStateException("Unable to find entity " + entityName);
}
}