Skip to content

Commit

Permalink
HHH-8276 - Integrate LoadPlans into UniqueEntityLoader (PoC)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Oct 9, 2013
1 parent f32c736 commit e1105fb
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
Expand Up @@ -44,8 +44,9 @@ public CollectionQuerySpaceImpl(
CollectionPersister persister,
String uid,
QuerySpacesImpl querySpaces,
boolean canJoinsBeRequired,
SessionFactoryImplementor sessionFactory) {
super( uid, Disposition.COLLECTION, querySpaces, sessionFactory );
super( uid, Disposition.COLLECTION, querySpaces, canJoinsBeRequired, sessionFactory );
this.persister = persister;
}

Expand All @@ -62,11 +63,13 @@ public PropertyMapping getPropertyMapping() {
public JoinImpl addIndexEntityJoin(
final EntityPersister indexPersister,
LoadPlanBuildingContext context) {
final boolean required = canJoinsBeRequired();
final String entityQuerySpaceUid = getQuerySpaces().generateImplicitUid();
final EntityQuerySpaceImpl entityQuerySpace = new EntityQuerySpaceImpl(
indexPersister,
entityQuerySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( entityQuerySpace );
Expand All @@ -77,7 +80,7 @@ public JoinImpl addIndexEntityJoin(
entityQuerySpace,
// not sure this 'rhsColumnNames' bit is correct...
( (Queryable) indexPersister ).getKeyColumnNames(),
false
required
);
internalGetJoins().add( join );

Expand All @@ -96,6 +99,7 @@ public JoinImpl addIndexCompositeJoin(
),
compositeQuerySpaceUid,
getQuerySpaces(),
canJoinsBeRequired(),
sessionFactory()
);
getQuerySpaces().registerQuerySpace( compositeQuerySpace );
Expand All @@ -105,7 +109,7 @@ public JoinImpl addIndexCompositeJoin(
"index",
compositeQuerySpace,
null,
false
canJoinsBeRequired()
);
internalGetJoins().add( join );

Expand All @@ -120,6 +124,7 @@ public JoinImpl addElementEntityJoin(
elementPersister,
entityQuerySpaceUid,
getQuerySpaces(),
canJoinsBeRequired(),
sessionFactory()
);
( (QuerySpacesImpl) context.getQuerySpaces() ).registerQuerySpace( entityQuerySpace );
Expand All @@ -130,7 +135,7 @@ public JoinImpl addElementEntityJoin(
"id",
entityQuerySpace,
( (Queryable) elementPersister ).getKeyColumnNames(),
false
canJoinsBeRequired()
);
internalGetJoins().add( join );

Expand All @@ -150,6 +155,7 @@ public Join addElementCompositeJoin(
),
compositeQuerySpaceUid,
getQuerySpaces(),
canJoinsBeRequired(),
sessionFactory()
);
( (QuerySpacesImpl) context.getQuerySpaces() ).registerQuerySpace( compositeQuerySpace );
Expand All @@ -160,7 +166,7 @@ public Join addElementCompositeJoin(
"elements",
compositeQuerySpace,
null,
false
canJoinsBeRequired()
);
internalGetJoins().add( join );

Expand Down
Expand Up @@ -45,20 +45,23 @@ public CompositeQuerySpaceImpl(
CompositePropertyMapping compositeSubPropertyMapping,
String uid,
QuerySpacesImpl querySpaces,
boolean canJoinsBeRequired,
SessionFactoryImplementor sessionFactory) {
super( uid, Disposition.COMPOSITE, querySpaces, sessionFactory );
super( uid, Disposition.COMPOSITE, querySpaces, canJoinsBeRequired, sessionFactory );
this.compositeSubPropertyMapping = compositeSubPropertyMapping;
}

public CompositeQuerySpaceImpl(
EntityQuerySpaceImpl entityQuerySpace,
CompositePropertyMapping compositePropertyMapping,
String uid) {
String uid,
boolean canJoinsBeRequired) {
// todo : we may need to keep around the owning entity query space to be able to properly handle circularity...
this(
compositePropertyMapping,
uid,
entityQuerySpace.getQuerySpaces(),
canJoinsBeRequired,
entityQuerySpace.sessionFactory()
);
}
Expand All @@ -71,11 +74,13 @@ public PropertyMapping getPropertyMapping() {
@Override
public JoinImpl addCompositeJoin(CompositionDefinition compositionDefinition, String querySpaceUid) {
final String propertyPath = compositionDefinition.getName();
final boolean required = canJoinsBeRequired() && !compositionDefinition.isNullable();

final CompositeQuerySpaceImpl rhs = new CompositeQuerySpaceImpl(
compositeSubPropertyMapping,
querySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( rhs );
Expand All @@ -85,7 +90,7 @@ public JoinImpl addCompositeJoin(CompositionDefinition compositionDefinition, St
propertyPath,
rhs,
null,
compositionDefinition.isNullable()
required
);
internalGetJoins().add( join );

Expand All @@ -98,10 +103,13 @@ public JoinImpl addEntityJoin(
EntityPersister persister,
String querySpaceUid,
boolean optional) {
final boolean required = canJoinsBeRequired() && !optional;

final EntityQuerySpaceImpl rhs = new EntityQuerySpaceImpl(
persister,
querySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( rhs );
Expand All @@ -115,7 +123,7 @@ public JoinImpl addEntityJoin(
(EntityType) attributeDefinition.getType(),
sessionFactory()
),
optional
required
);
internalGetJoins().add( join );

Expand All @@ -127,10 +135,13 @@ public JoinImpl addCollectionJoin(
AttributeDefinition attributeDefinition,
CollectionPersister collectionPersister,
String querySpaceUid) {
final boolean required = canJoinsBeRequired() && ! attributeDefinition.isNullable();

final CollectionQuerySpaceImpl rhs = new CollectionQuerySpaceImpl(
collectionPersister,
querySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( rhs );
Expand All @@ -140,7 +151,7 @@ public JoinImpl addCollectionJoin(
attributeDefinition.getName(),
rhs,
( (CollectionType) attributeDefinition.getType() ).getAssociatedJoinable( sessionFactory() ).getKeyColumnNames(),
attributeDefinition.isNullable()
required
);
internalGetJoins().add( join );

Expand Down
Expand Up @@ -47,8 +47,9 @@ public EntityQuerySpaceImpl(
EntityPersister persister,
String uid,
QuerySpacesImpl querySpaces,
boolean canJoinsBeRequired,
SessionFactoryImplementor sessionFactory) {
super( uid, Disposition.ENTITY, querySpaces, sessionFactory );
super( uid, Disposition.ENTITY, querySpaces, canJoinsBeRequired, sessionFactory );
this.persister = persister;
}

Expand All @@ -71,6 +72,8 @@ public EntityPersister getEntityPersister() {

@Override
public JoinImpl addCompositeJoin(CompositionDefinition compositionDefinition, String querySpaceUid) {
final boolean required = canJoinsBeRequired() && !compositionDefinition.isNullable();

final CompositeQuerySpaceImpl rhs = new CompositeQuerySpaceImpl(
new CompositePropertyMapping(
compositionDefinition.getType(),
Expand All @@ -79,6 +82,7 @@ public JoinImpl addCompositeJoin(CompositionDefinition compositionDefinition, St
),
querySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( rhs );
Expand All @@ -88,7 +92,7 @@ public JoinImpl addCompositeJoin(CompositionDefinition compositionDefinition, St
compositionDefinition.getName(),
rhs,
null,
compositionDefinition.isNullable()
required
);
internalGetJoins().add( join );

Expand All @@ -100,10 +104,13 @@ public JoinImpl addEntityJoin(
EntityPersister persister,
String querySpaceUid,
boolean optional) {
final boolean required = canJoinsBeRequired() && !optional;

final EntityQuerySpaceImpl rhs = new EntityQuerySpaceImpl(
persister,
querySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( rhs );
Expand All @@ -116,7 +123,7 @@ public JoinImpl addEntityJoin(
(EntityType) attribute.getType(),
sessionFactory()
),
optional
required
);
internalGetJoins().add( join );

Expand All @@ -128,10 +135,13 @@ public JoinImpl addCollectionJoin(
AttributeDefinition attributeDefinition,
CollectionPersister collectionPersister,
String querySpaceUid) {
final boolean required = canJoinsBeRequired() && !attributeDefinition.isNullable();

final CollectionQuerySpaceImpl rhs = new CollectionQuerySpaceImpl(
collectionPersister,
querySpaceUid,
getQuerySpaces(),
required,
sessionFactory()
);
getQuerySpaces().registerQuerySpace( rhs );
Expand All @@ -141,7 +151,7 @@ public JoinImpl addCollectionJoin(
attributeDefinition.getName(),
rhs,
( (CollectionType) attributeDefinition.getType() ).getAssociatedJoinable( sessionFactory() ).getKeyColumnNames(),
attributeDefinition.isNullable()
required
);
internalGetJoins().add( join );

Expand All @@ -158,7 +168,8 @@ public Join makeCompositeIdentifierJoin() {
(PropertyMapping) getEntityPersister(),
getEntityPersister().getIdentifierPropertyName()
),
compositeQuerySpaceUid
compositeQuerySpaceUid,
canJoinsBeRequired()
);
getQuerySpaces().registerQuerySpace( rhs );

Expand All @@ -167,7 +178,7 @@ public Join makeCompositeIdentifierJoin() {
"id",
rhs,
null,
false
canJoinsBeRequired()
);
internalGetJoins().add( join );

Expand Down
Expand Up @@ -69,7 +69,7 @@ public QuerySpace getRightHandSide() {
}

@Override
public boolean isRightHandSideOptional() {
public boolean isRightHandSideRequired() {
return rightHandSideOptional;
}

Expand Down
Expand Up @@ -95,6 +95,7 @@ public EntityQuerySpace makeEntityQuerySpace(String uid, EntityPersister entityP
entityPersister,
uid,
this,
true,
sessionFactory
);
registerQuerySpace( space );
Expand All @@ -113,6 +114,7 @@ public CollectionQuerySpaceImpl makeCollectionQuerySpace(String uid, CollectionP
collectionPersister,
uid,
this,
true,
sessionFactory
);
registerQuerySpace( space );
Expand Down
Expand Up @@ -42,18 +42,21 @@ public abstract class AbstractQuerySpace extends AbstractPlanNode implements Que
private final String uid;
private final Disposition disposition;
private final QuerySpacesImpl querySpaces;
private final boolean canJoinsBeRequired;

private List<Join> joins;

public AbstractQuerySpace(
String uid,
Disposition disposition,
QuerySpacesImpl querySpaces,
boolean canJoinsBeRequired,
SessionFactoryImplementor sessionFactory) {
super( sessionFactory );
this.uid = uid;
this.disposition = disposition;
this.querySpaces = querySpaces;
this.canJoinsBeRequired = canJoinsBeRequired;
}

protected SessionFactoryImplementor sessionFactory() {
Expand All @@ -63,6 +66,15 @@ protected SessionFactoryImplementor sessionFactory() {
// todo : copy ctor - that depends how graph copying works here...


/**
* Can any joins created from here (with this as the left-hand side) be required joins?
*
* @return {@code true} indicates joins can be required; {@code false} indicates they cannot.
*/
public boolean canJoinsBeRequired() {
return canJoinsBeRequired;
}

/**
* Provides subclasses access to the spaces to which this space belongs.
*
Expand Down
Expand Up @@ -179,7 +179,7 @@ private void renderEntityJoin(Join join, JoinFragment joinFragment) {
addJoins(
joinFragment,
joinable,
join.isRightHandSideOptional() ? JoinType.LEFT_OUTER_JOIN : JoinType.INNER_JOIN,
join.isRightHandSideRequired() ? JoinType.INNER_JOIN : JoinType.LEFT_OUTER_JOIN,
aliases.getTableAlias(),
rhsColumnNames,
aliasedLhsColumnNames,
Expand Down
Expand Up @@ -40,7 +40,7 @@ public interface Join {

public QuerySpace getRightHandSide();

public boolean isRightHandSideOptional();
public boolean isRightHandSideRequired();

// Ugh! This part will unfortunately be SQL specific :( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
Expand Up @@ -25,6 +25,11 @@

import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;

import org.hibernate.test.annotations.Country;
import org.hibernate.test.annotations.collectionelement.Boy;
import org.hibernate.test.annotations.collectionelement.Matrix;
import org.hibernate.test.annotations.collectionelement.TestCourse;
import org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest;

//import org.hibernate.loader.plan2.spi.BidirectionalEntityFetch;
Expand Down Expand Up @@ -255,6 +260,23 @@ public void testManyToMany() {

}

@Test
public void testAnotherBasicCollection() {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass( Boy.class );
cfg.addAnnotatedClass( Country.class );
cfg.addAnnotatedClass( TestCourse.class );
cfg.addAnnotatedClass( Matrix.class );
SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();

try {
doCompare( sf, (OuterJoinLoadable) sf.getClassMetadata( Boy.class ) );
}
finally {
sf.close();
}
}

private void doCompare(SessionFactoryImplementor sf, OuterJoinLoadable persister) {
LoadPlanStructureAssertionHelper.INSTANCE.performBasicComparison( sf, persister );
}
Expand Down

0 comments on commit e1105fb

Please sign in to comment.