add 'Criteria' testing
This commit is contained in:
parent
e2ddd800ac
commit
a9c2ea3a46
|
@ -6,8 +6,463 @@
|
|||
*/
|
||||
package org.hibernate.orm.test.query.criteria.entitygraph;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Embedded;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.hibernate.engine.spi.EffectiveEntityGraph;
|
||||
import org.hibernate.engine.spi.LoadQueryInfluencers;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.graph.GraphSemantic;
|
||||
import org.hibernate.graph.spi.RootGraphImplementor;
|
||||
import org.hibernate.metamodel.mapping.EntityValuedModelPart;
|
||||
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
|
||||
import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
|
||||
import org.hibernate.query.hql.spi.HqlQueryImplementor;
|
||||
import org.hibernate.query.spi.QueryImplementor;
|
||||
import org.hibernate.query.sqm.internal.QuerySqmImpl;
|
||||
import org.hibernate.query.sqm.sql.SqmSelectTranslation;
|
||||
import org.hibernate.query.sqm.sql.internal.StandardSqmSelectTranslator;
|
||||
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
|
||||
import org.hibernate.sql.ast.tree.from.CompositeTableGroup;
|
||||
import org.hibernate.sql.ast.tree.from.FromClause;
|
||||
import org.hibernate.sql.ast.tree.from.StandardTableGroup;
|
||||
import org.hibernate.sql.ast.tree.from.TableGroup;
|
||||
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
|
||||
import org.hibernate.sql.ast.tree.select.SelectStatement;
|
||||
import org.hibernate.sql.results.graph.DomainResult;
|
||||
import org.hibernate.sql.results.graph.Fetch;
|
||||
import org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch;
|
||||
import org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl;
|
||||
import org.hibernate.sql.results.graph.entity.EntityFetch;
|
||||
import org.hibernate.sql.results.graph.entity.EntityResult;
|
||||
import org.hibernate.sql.results.graph.entity.internal.EntityFetchDelayedImpl;
|
||||
import org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
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.Test;
|
||||
import org.junit.platform.commons.util.CollectionUtils;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hibernate.testing.hamcrest.AssignableMatcher.assignableTo;
|
||||
import static org.hibernate.testing.hamcrest.CollectionMatchers.hasSize;
|
||||
import static org.hibernate.testing.hamcrest.CollectionMatchers.isEmpty;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
@DomainModel(
|
||||
annotatedClasses = {
|
||||
CriteriaEntityGraphTest.Cat.class,
|
||||
CriteriaEntityGraphTest.Person.class,
|
||||
CriteriaEntityGraphTest.Country.class,
|
||||
CriteriaEntityGraphTest.Dog.class,
|
||||
CriteriaEntityGraphTest.ExpressCompany.class
|
||||
}
|
||||
)
|
||||
@SessionFactory
|
||||
@TestForIssue( jiraKey = "HHH-13756" )
|
||||
public class CriteriaEntityGraphTest {
|
||||
|
||||
@Test
|
||||
void testBasicFetchSemantics(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEmptyJoinedGroup( sqlAst );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class,
|
||||
entityFetch -> assertThat( entityFetch, instanceOf( EntityFetchDelayedImpl.class ) )
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFetchSemanticsWithSubgraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
eg.addSubgraph( "owner", Person.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> {} );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFetchSemanticsWithDeepSubgraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
eg.addSubgraph( "owner", Person.class ).addSubgraph( "company", ExpressCompany.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, tableGroup -> {
|
||||
Set<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
|
||||
Map<String, Class<? extends TableGroup>> tableGroupByName = tableGroupJoins.stream()
|
||||
.map( TableGroupJoin::getJoinedGroup )
|
||||
.collect( Collectors.toMap(
|
||||
tg -> tg.getModelPart().getPartName(),
|
||||
TableGroup::getClass
|
||||
) );
|
||||
Map<String, Class<? extends TableGroup> > expectedTableGroupByName = new HashMap<>();
|
||||
expectedTableGroupByName.put( "homeAddress", CompositeTableGroup.class );
|
||||
expectedTableGroupByName.put( "company", StandardTableGroup.class );
|
||||
assertThat( tableGroupByName, is( expectedTableGroupByName ) );
|
||||
} );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
|
||||
assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) );
|
||||
final EntityResult ownerEntityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult();
|
||||
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = ownerEntityResult.getFetches()
|
||||
.stream().collect( Collectors.toMap(
|
||||
fetch -> fetch.getFetchedMapping().getPartName(),
|
||||
Fetch::getClass
|
||||
) );
|
||||
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
|
||||
expectedFetchClassByAttributeName.put( "homeAddress", EmbeddableFetchImpl.class );
|
||||
expectedFetchClassByAttributeName.put( "pets", DelayedCollectionFetch.class );
|
||||
expectedFetchClassByAttributeName.put( "company", EntityFetchJoinedImpl.class );
|
||||
assertThat( fetchClassByAttributeName, is( expectedFetchClassByAttributeName ) );
|
||||
|
||||
final Fetch companyFetch = ownerEntityResult.findFetch( "company" );
|
||||
assertThat( companyFetch, notNullValue() );
|
||||
|
||||
final EntityResult companyEntityResult = ( (EntityFetchJoinedImpl) companyFetch).getEntityResult();
|
||||
assertThat( companyEntityResult.getFetches(), hasSize( 1 ) );
|
||||
|
||||
final Fetch shipAddressesFetch = companyEntityResult.getFetches().get( 0 );
|
||||
assertThat( shipAddressesFetch.getFetchedMapping().getPartName(), is( "shipAddresses" ) );
|
||||
assertThat( shipAddressesFetch, instanceOf( DelayedCollectionFetch.class ) );
|
||||
} );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicLoadSemantics(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, eg, GraphSemantic.LOAD, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEmptyJoinedGroup( sqlAst );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class,
|
||||
entityFetch -> assertThat( entityFetch, instanceOf( EntityFetchDelayedImpl.class ) ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoadLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
eg.addSubgraph( "owner", Person.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, eg, GraphSemantic.LOAD, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
|
||||
assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) );
|
||||
final EntityResult entityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult();
|
||||
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = entityResult.getFetches().stream().collect( Collectors.toMap(
|
||||
fetch -> fetch.getFetchedMapping().getPartName(),
|
||||
Fetch::getClass
|
||||
) );
|
||||
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
|
||||
expectedFetchClassByAttributeName.put( "pets", DelayedCollectionFetch.class );
|
||||
expectedFetchClassByAttributeName.put( "homeAddress", EmbeddableFetchImpl.class );
|
||||
expectedFetchClassByAttributeName.put( "company", EntityFetchDelayedImpl.class );
|
||||
assertThat( fetchClassByAttributeName, is( expectedFetchClassByAttributeName ) );
|
||||
} );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicElementCollectionsLoadGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Dog> eg = session.createEntityGraph( Dog.class );
|
||||
eg.addAttributeNodes( "favorites" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Dog.class, eg, GraphSemantic.LOAD, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertPluralAttributeJoinedGroup( sqlAst, "favorites", tableGroup -> {} );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicElementCollectionsFetchGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<Dog> eg = session.createEntityGraph( Dog.class );
|
||||
eg.addAttributeNodes( "favorites" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Dog.class, eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertPluralAttributeJoinedGroup( sqlAst, "favorites", tableGroup -> {} );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmbeddedCollectionLoadGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<ExpressCompany> eg = session.createEntityGraph( ExpressCompany.class );
|
||||
eg.addAttributeNodes( "shipAddresses" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst(
|
||||
ExpressCompany.class,
|
||||
eg, GraphSemantic.LOAD,
|
||||
session
|
||||
);
|
||||
|
||||
// Check the from-clause
|
||||
assertPluralAttributeJoinedGroup( sqlAst, "shipAddresses", tableGroup -> {
|
||||
assertThat( tableGroup.getTableGroupJoins(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup compositeTableGroup = tableGroup.getTableGroupJoins().iterator().next().getJoinedGroup();
|
||||
assertThat( compositeTableGroup, instanceOf( CompositeTableGroup.class ) );
|
||||
assertThat( compositeTableGroup.getTableGroupJoins(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup countryTableGroup = compositeTableGroup.getTableGroupJoins().iterator().next().getJoinedGroup();
|
||||
assertThat( countryTableGroup.getModelPart().getPartName(), is( "country" ) );
|
||||
|
||||
assertThat( countryTableGroup.getTableGroupJoins(), isEmpty() );
|
||||
} );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmbeddedCollectionFetchGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<ExpressCompany> eg = session.createEntityGraph( ExpressCompany.class );
|
||||
eg.addAttributeNodes( "shipAddresses" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst(
|
||||
ExpressCompany.class,
|
||||
eg, GraphSemantic.FETCH,
|
||||
session
|
||||
);
|
||||
|
||||
// Check the from-clause
|
||||
assertPluralAttributeJoinedGroup( sqlAst, "shipAddresses", tableGroup -> {
|
||||
assertThat( tableGroup.getTableGroupJoins(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup compositeTableGroup = CollectionUtils.getOnlyElement( tableGroup.getTableGroupJoins() ).getJoinedGroup();
|
||||
assertThat( compositeTableGroup, instanceOf( CompositeTableGroup.class ) );
|
||||
assertThat( compositeTableGroup.getTableGroupJoins(), isEmpty() );
|
||||
} );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// util methods for verifying 'from-clause' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
private void assertEmptyJoinedGroup(SelectStatement sqlAst) {
|
||||
final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
|
||||
assertThat( fromClause.getRoots(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup rootTableGroup = fromClause.getRoots().get( 0 );
|
||||
assertThat( rootTableGroup.getTableGroupJoins(), isEmpty() );
|
||||
}
|
||||
|
||||
private void assertEntityValuedJoinedGroup(SelectStatement sqlAst, String expectedAttributeName, Class<?> expectedEntityJpaClass, Consumer<TableGroup> tableGroupConsumer) {
|
||||
final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
|
||||
assertThat( fromClause.getRoots(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup rootTableGroup = fromClause.getRoots().get( 0 );
|
||||
assertThat( rootTableGroup.getTableGroupJoins(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup joinedGroup = rootTableGroup.getTableGroupJoins().iterator().next().getJoinedGroup();
|
||||
assertThat( joinedGroup.getModelPart().getPartName(), is( expectedAttributeName ) );
|
||||
assertThat( joinedGroup.getModelPart().getJavaTypeDescriptor().getJavaType(), assignableTo( expectedEntityJpaClass ) );
|
||||
assertThat( joinedGroup.getModelPart(), instanceOf( EntityValuedModelPart.class ) );
|
||||
|
||||
tableGroupConsumer.accept( joinedGroup );
|
||||
}
|
||||
|
||||
private void assertPluralAttributeJoinedGroup(SelectStatement sqlAst, String expectedPluralAttributeName, Consumer<TableGroup> tableGroupConsumer) {
|
||||
final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
|
||||
assertThat( fromClause.getRoots(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup root = fromClause.getRoots().get( 0 );
|
||||
assertThat( root.getTableGroupJoins(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup joinedGroup = root.getTableGroupJoins().iterator().next().getJoinedGroup();
|
||||
assertThat( joinedGroup.getModelPart().getPartName(), is( expectedPluralAttributeName ) );
|
||||
assertThat( joinedGroup.getModelPart(), instanceOf( PluralAttributeMapping.class ) );
|
||||
tableGroupConsumer.accept( joinedGroup );
|
||||
}
|
||||
|
||||
private void assertPersonHomeAddressJoinedGroup(TableGroup tableGroup) {
|
||||
assertThat( tableGroup.getTableGroupJoins(), hasSize( 1 ) );
|
||||
|
||||
final TableGroup joinedGroup = tableGroup.getTableGroupJoins().iterator().next().getJoinedGroup();
|
||||
assertThat( joinedGroup.getModelPart().getPartName(), is( "homeAddress" ) );
|
||||
assertThat( joinedGroup.getModelPart(), instanceOf( EmbeddedAttributeMapping.class ) );
|
||||
assertThat( joinedGroup, instanceOf( CompositeTableGroup.class ) );
|
||||
}
|
||||
|
||||
// util methods for verifying 'domain-result' graph ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
private void assertDomainResult(SelectStatement sqlAst,
|
||||
Class<?> expectedEntityJpaClass,
|
||||
String expectedAttributeName,
|
||||
Class<?> expectedAttributeEntityJpaClass,
|
||||
Consumer<EntityFetch> entityFetchConsumer) {
|
||||
assertThat( sqlAst.getDomainResultDescriptors(), hasSize( 1 ) );
|
||||
|
||||
final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get( 0 );
|
||||
assertThat( domainResult, instanceOf( EntityResult.class ) );
|
||||
|
||||
final EntityResult entityResult = (EntityResult) domainResult;
|
||||
assertThat( entityResult.getReferencedModePart().getJavaTypeDescriptor().getJavaType(), assignableTo( expectedEntityJpaClass ) );
|
||||
assertThat( entityResult.getFetches(), hasSize( 1 ) );
|
||||
|
||||
final Fetch fetch = entityResult.getFetches().get( 0 );
|
||||
assertThat( fetch, instanceOf( EntityFetch.class ) );
|
||||
|
||||
final EntityFetch entityFetch = (EntityFetch) fetch;
|
||||
assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( expectedAttributeName ) );
|
||||
assertThat( entityFetch.getReferencedModePart().getJavaTypeDescriptor().getJavaType(), assignableTo( expectedAttributeEntityJpaClass ) );
|
||||
|
||||
entityFetchConsumer.accept( entityFetch );
|
||||
}
|
||||
|
||||
private <T> SelectStatement buildSqlSelectAst(
|
||||
Class<T> entityType,
|
||||
RootGraphImplementor<T> entityGraph,
|
||||
GraphSemantic mode,
|
||||
SessionImplementor session) {
|
||||
|
||||
final LoadQueryInfluencers loadQueryInfluencers = new LoadQueryInfluencers( session.getSessionFactory() );
|
||||
final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
|
||||
effectiveEntityGraph.applyGraph( entityGraph, mode );
|
||||
|
||||
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery( entityType );
|
||||
criteriaQuery.select( criteriaQuery.from( entityType ) );
|
||||
|
||||
final QueryImplementor<T> query = session.createQuery( criteriaQuery );
|
||||
final HqlQueryImplementor<String> hqlQuery = (HqlQueryImplementor<String>) query;
|
||||
|
||||
final SqmSelectStatement sqmStatement = (SqmSelectStatement) hqlQuery.getSqmStatement();
|
||||
|
||||
final StandardSqmSelectTranslator sqmConverter = new StandardSqmSelectTranslator(
|
||||
hqlQuery.getQueryOptions(),
|
||||
( (QuerySqmImpl) hqlQuery ).getDomainParameterXref(),
|
||||
query.getParameterBindings(),
|
||||
loadQueryInfluencers,
|
||||
session.getSessionFactory()
|
||||
);
|
||||
|
||||
final SqmSelectTranslation sqmInterpretation = sqmConverter.translate( sqmStatement );
|
||||
return sqmInterpretation.getSqlAst();
|
||||
}
|
||||
|
||||
@Entity(name = "Dog")
|
||||
public static class Dog {
|
||||
@Id
|
||||
String name;
|
||||
|
||||
@ElementCollection
|
||||
Set<String> favorites;
|
||||
}
|
||||
|
||||
@Entity(name = "Cat")
|
||||
public static class Cat {
|
||||
@Id
|
||||
String name;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
Person owner;
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
@Id
|
||||
String name;
|
||||
|
||||
@OneToMany(mappedBy = "owner")
|
||||
Set<Cat> pets;
|
||||
|
||||
@Embedded
|
||||
Address homeAddress;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
ExpressCompany company;
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class Address {
|
||||
@ManyToOne
|
||||
Country country;
|
||||
}
|
||||
|
||||
@Entity(name = "ExpressCompany")
|
||||
public static class ExpressCompany {
|
||||
@Id
|
||||
String name;
|
||||
|
||||
@ElementCollection
|
||||
Set<Address> shipAddresses;
|
||||
}
|
||||
|
||||
@Entity(name = "Country")
|
||||
public static class Country {
|
||||
@Id
|
||||
String name;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,15 +84,15 @@ public class HqlEntityGraphTest {
|
|||
void testBasicFetchSemantics(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat c", eg, GraphSemantic.FETCH, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, "select c from Cat c", eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEmptyJoinedGroup( sqlAst );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", Person.class,
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class,
|
||||
entityFetch -> assertThat( entityFetch, instanceOf( EntityFetchDelayedImpl.class ) )
|
||||
);
|
||||
}
|
||||
|
@ -103,16 +103,16 @@ public class HqlEntityGraphTest {
|
|||
void testFetchSemanticsWithSubgraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
|
||||
eg.addSubgraph( "owner", HqlEntityGraphTest.Person.class );
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
eg.addSubgraph( "owner", Person.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", HqlEntityGraphTest.Person.class, this::assertPersonHomeAddressJoinedGroup );
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class, entityFetch -> {} );
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> {} );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -121,13 +121,13 @@ public class HqlEntityGraphTest {
|
|||
void testFetchSemanticsWithDeepSubgraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
|
||||
eg.addSubgraph( "owner", HqlEntityGraphTest.Person.class ).addSubgraph( "company", HqlEntityGraphTest.ExpressCompany.class );
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
eg.addSubgraph( "owner", Person.class ).addSubgraph( "company", ExpressCompany.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", HqlEntityGraphTest.Person.class, tableGroup -> {
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, tableGroup -> {
|
||||
Set<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
|
||||
Map<String, Class<? extends TableGroup>> tableGroupByName = tableGroupJoins.stream()
|
||||
.map( TableGroupJoin::getJoinedGroup )
|
||||
|
@ -142,7 +142,7 @@ public class HqlEntityGraphTest {
|
|||
} );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class, entityFetch -> {
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
|
||||
assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) );
|
||||
final EntityResult ownerEntityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult();
|
||||
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = ownerEntityResult.getFetches()
|
||||
|
@ -174,15 +174,15 @@ public class HqlEntityGraphTest {
|
|||
void testBasicLoadSemantics(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.LOAD, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, "select c from Cat as c", eg, GraphSemantic.LOAD, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEmptyJoinedGroup( sqlAst );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class,
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class,
|
||||
entityFetch -> assertThat( entityFetch, instanceOf( EntityFetchDelayedImpl.class ) ) );
|
||||
}
|
||||
);
|
||||
|
@ -192,16 +192,16 @@ public class HqlEntityGraphTest {
|
|||
void testLoadLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
|
||||
eg.addSubgraph( "owner", HqlEntityGraphTest.Person.class );
|
||||
final RootGraphImplementor<Cat> eg = session.createEntityGraph( Cat.class );
|
||||
eg.addSubgraph( "owner", Person.class );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.LOAD, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Cat.class, "select c from Cat as c", eg, GraphSemantic.LOAD, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", HqlEntityGraphTest.Person.class, this::assertPersonHomeAddressJoinedGroup );
|
||||
assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup );
|
||||
|
||||
// Check the domain-result graph
|
||||
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class, entityFetch -> {
|
||||
assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
|
||||
assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) );
|
||||
final EntityResult entityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult();
|
||||
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = entityResult.getFetches().stream().collect( Collectors.toMap(
|
||||
|
@ -222,10 +222,10 @@ public class HqlEntityGraphTest {
|
|||
void testBasicElementCollectionsLoadGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Dog> eg = session.createEntityGraph( HqlEntityGraphTest.Dog.class );
|
||||
final RootGraphImplementor<Dog> eg = session.createEntityGraph( Dog.class );
|
||||
eg.addAttributeNodes( "favorites" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Dog.class, "select d from Dog as d", eg, GraphSemantic.LOAD, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Dog.class, "select d from Dog as d", eg, GraphSemantic.LOAD, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertPluralAttributeJoinedGroup( sqlAst, "favorites", tableGroup -> {} );
|
||||
|
@ -237,10 +237,10 @@ public class HqlEntityGraphTest {
|
|||
void testBasicElementCollectionsFetchGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.Dog> eg = session.createEntityGraph( HqlEntityGraphTest.Dog.class );
|
||||
final RootGraphImplementor<Dog> eg = session.createEntityGraph( Dog.class );
|
||||
eg.addAttributeNodes( "favorites" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Dog.class, "select d from Dog as d", eg, GraphSemantic.FETCH, session );
|
||||
final SelectStatement sqlAst = buildSqlSelectAst( Dog.class, "select d from Dog as d", eg, GraphSemantic.FETCH, session );
|
||||
|
||||
// Check the from-clause
|
||||
assertPluralAttributeJoinedGroup( sqlAst, "favorites", tableGroup -> {} );
|
||||
|
@ -252,11 +252,11 @@ public class HqlEntityGraphTest {
|
|||
void testEmbeddedCollectionLoadGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.ExpressCompany> eg = session.createEntityGraph( HqlEntityGraphTest.ExpressCompany.class );
|
||||
final RootGraphImplementor<ExpressCompany> eg = session.createEntityGraph( ExpressCompany.class );
|
||||
eg.addAttributeNodes( "shipAddresses" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst(
|
||||
HqlEntityGraphTest.ExpressCompany.class,
|
||||
ExpressCompany.class,
|
||||
"select company from ExpressCompany as company",
|
||||
eg, GraphSemantic.LOAD,
|
||||
session
|
||||
|
@ -284,11 +284,11 @@ public class HqlEntityGraphTest {
|
|||
void testEmbeddedCollectionFetchGraph(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
final RootGraphImplementor<HqlEntityGraphTest.ExpressCompany> eg = session.createEntityGraph( HqlEntityGraphTest.ExpressCompany.class );
|
||||
final RootGraphImplementor<ExpressCompany> eg = session.createEntityGraph( ExpressCompany.class );
|
||||
eg.addAttributeNodes( "shipAddresses" );
|
||||
|
||||
final SelectStatement sqlAst = buildSqlSelectAst(
|
||||
HqlEntityGraphTest.ExpressCompany.class,
|
||||
ExpressCompany.class,
|
||||
"select company from ExpressCompany as company",
|
||||
eg, GraphSemantic.FETCH,
|
||||
session
|
||||
|
|
Loading…
Reference in New Issue