Skip to content

Commit 3600ffb

Browse files
committed
HHH-7841 - Redesign Loader
1 parent b8b9735 commit 3600ffb

19 files changed

+858
-199
lines changed

hibernate-core/src/main/java/org/hibernate/loader/internal/ResultSetProcessorImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public List extractResults(
9090
List<AfterLoadAction> afterLoadActionList) throws SQLException {
9191

9292
final LoadPlan loadPlan = loadPlanAdvisor.advise( this.baseLoadPlan );
93+
if ( loadPlan == null ) {
94+
throw new IllegalStateException( "LoadPlanAdvisor returned null" );
95+
}
9396

9497
handlePotentiallyEmptyCollectionRootReturns( loadPlan, queryParameters.getCollectionKeys(), resultSet, session );
9598

hibernate-core/src/main/java/org/hibernate/loader/plan/spi/CompositeFetch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class CompositeFetch extends AbstractSingularAttributeFetch {
4646
public CompositeFetch(
4747
SessionFactoryImplementor sessionFactory,
4848
String alias,
49-
AbstractFetchOwner owner,
49+
FetchOwner owner,
5050
String ownerProperty) {
5151
super( sessionFactory, alias, LockMode.NONE, owner, ownerProperty, FETCH_PLAN );
5252
}

hibernate-core/src/main/java/org/hibernate/loader/spi/LoadPlanAdvisor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,24 @@
2626
import org.hibernate.loader.plan.spi.LoadPlan;
2727

2828
/**
29+
* An advisor that can be made available to the {@link ResultSetProcessor} and {@link ScrollableResultSetProcessor}.
30+
*
31+
* The processors consult with the advisor, if one is provided, as a means to influence the load plan, meaning that
32+
* the advisor might add fetches. A caveat is that any added fetches cannot be join fetches (they cannot alter the
33+
* SQL); if a fetch is added as {@link org.hibernate.engine.FetchTiming#IMMEDIATE}, it must be a "subsequent form":
34+
* {@link org.hibernate.engine.FetchStyle#SELECT}, {@link org.hibernate.engine.FetchStyle#SUBSELECT},
35+
* {@link org.hibernate.engine.FetchStyle#BATCH}.
36+
*
2937
* @author Steve Ebersole
3038
*/
3139
public interface LoadPlanAdvisor {
40+
/**
41+
* Advise on the given LoadPlan, returning a new LoadPlan if any additions are needed. It is the responsibility
42+
* of the advisor to return the original load plan if no additions were needed
43+
*
44+
* @param loadPlan The load plan to advise on.
45+
*
46+
* @return The original or advised load plan.
47+
*/
3248
public LoadPlan advise(LoadPlan loadPlan);
3349
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.graph.internal.advisor;
25+
26+
import org.hibernate.LockMode;
27+
import org.hibernate.engine.FetchStrategy;
28+
import org.hibernate.engine.FetchStyle;
29+
import org.hibernate.engine.FetchTiming;
30+
import org.hibernate.engine.spi.SessionFactoryImplementor;
31+
import org.hibernate.jpa.graph.spi.AttributeNodeImplementor;
32+
import org.hibernate.loader.plan.spi.CollectionFetch;
33+
import org.hibernate.loader.plan.spi.CompositeFetch;
34+
import org.hibernate.loader.plan.spi.EntityFetch;
35+
import org.hibernate.loader.plan.spi.Fetch;
36+
import org.hibernate.loader.plan.spi.FetchOwner;
37+
38+
/**
39+
* @author Steve Ebersole
40+
*/
41+
public class AdviceHelper {
42+
private AdviceHelper() {
43+
}
44+
45+
static Fetch buildFetch(FetchOwner fetchOwner, AttributeNodeImplementor attributeNode) {
46+
if ( attributeNode.getAttribute().isAssociation() ) {
47+
if ( attributeNode.getAttribute().isCollection() ) {
48+
return new CollectionFetch(
49+
(SessionFactoryImplementor) attributeNode.entityManagerFactory().getSessionFactory(),
50+
"abc-xyz", // alias
51+
LockMode.NONE,
52+
fetchOwner,
53+
new FetchStrategy( FetchTiming.IMMEDIATE, FetchStyle.SELECT ),
54+
attributeNode.getAttributeName(),
55+
null, // sql table alias
56+
null // entityaliases
57+
);
58+
}
59+
else {
60+
return new EntityFetch(
61+
(SessionFactoryImplementor) attributeNode.entityManagerFactory().getSessionFactory(),
62+
"abc-xyz", // alias
63+
LockMode.NONE,
64+
fetchOwner,
65+
attributeNode.getAttributeName(),
66+
new FetchStrategy( FetchTiming.IMMEDIATE, FetchStyle.SELECT ),
67+
null, // sql table alias
68+
null // entityaliases
69+
);
70+
}
71+
}
72+
else {
73+
return new CompositeFetch(
74+
(SessionFactoryImplementor) attributeNode.entityManagerFactory().getSessionFactory(),
75+
"abc-xyz", // alias
76+
fetchOwner,
77+
attributeNode.getAttributeName()
78+
);
79+
}
80+
}
81+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.graph.internal.advisor;
25+
26+
/**
27+
* Links together the LoadPlan graph and JPA graph notions of the same node.
28+
*
29+
* @author Steve Ebersole
30+
*/
31+
interface AdviceNodeDescriptor {
32+
public JpaGraphReference attributeProcessed(String attributeName);
33+
34+
public void applyMissingFetches();
35+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.graph.internal.advisor;
25+
26+
import org.hibernate.loader.plan.spi.CollectionFetch;
27+
28+
/**
29+
* @author Steve Ebersole
30+
*/
31+
public class AdviceNodeDescriptorCollectionReference implements AdviceNodeDescriptor {
32+
private final CollectionFetch collectionFetch;
33+
private final JpaGraphReference jpaGraphReference;
34+
35+
public AdviceNodeDescriptorCollectionReference(
36+
CollectionFetch collectionFetch,
37+
JpaGraphReference jpaGraphReference) {
38+
//To change body of created methods use File | Settings | File Templates.
39+
this.collectionFetch = collectionFetch;
40+
this.jpaGraphReference = jpaGraphReference;
41+
}
42+
43+
@Override
44+
public JpaGraphReference attributeProcessed(String attributeName) {
45+
return jpaGraphReference != null
46+
? jpaGraphReference.attributeProcessed( attributeName )
47+
: null;
48+
}
49+
50+
@Override
51+
public void applyMissingFetches() {
52+
if ( jpaGraphReference == null ) {
53+
return;
54+
}
55+
jpaGraphReference.applyMissingFetches( collectionFetch.getElementGraph() );
56+
( (JpaGraphCollectionReference) jpaGraphReference ).applyMissingKeyFetches( collectionFetch.getIndexGraph() );
57+
58+
}
59+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.graph.internal.advisor;
25+
26+
import org.hibernate.loader.plan.spi.CompositeFetch;
27+
import org.hibernate.loader.plan.spi.FetchOwner;
28+
29+
/**
30+
* @author Steve Ebersole
31+
*/
32+
public class AdviceNodeDescriptorCompositeReference implements AdviceNodeDescriptor {
33+
private final FetchOwner fetchOwner;
34+
private final JpaGraphReference jpaGraphReference;
35+
36+
AdviceNodeDescriptorCompositeReference(CompositeFetch fetchOwner, JpaGraphReference jpaGraphReference) {
37+
this.fetchOwner = fetchOwner;
38+
this.jpaGraphReference = jpaGraphReference;
39+
}
40+
41+
@Override
42+
public JpaGraphReference attributeProcessed(String attributeName) {
43+
return jpaGraphReference != null
44+
? jpaGraphReference.attributeProcessed( attributeName )
45+
: null;
46+
}
47+
48+
@Override
49+
public void applyMissingFetches() {
50+
if ( jpaGraphReference != null ) {
51+
jpaGraphReference.applyMissingFetches( fetchOwner );
52+
}
53+
}
54+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.graph.internal.advisor;
25+
26+
import org.hibernate.loader.plan.spi.EntityFetch;
27+
import org.hibernate.loader.plan.spi.EntityReturn;
28+
import org.hibernate.loader.plan.spi.FetchOwner;
29+
30+
/**
31+
* An AdviceNodeDescriptor that represents an entity reference
32+
*
33+
* @author Steve Ebersole
34+
*/
35+
class AdviceNodeDescriptorEntityReference implements AdviceNodeDescriptor {
36+
private final FetchOwner fetchOwner;
37+
private final JpaGraphReference jpaGraphReference;
38+
39+
AdviceNodeDescriptorEntityReference(EntityReturn fetchOwner, JpaGraphReference jpaGraphReference) {
40+
this.fetchOwner = fetchOwner;
41+
this.jpaGraphReference = jpaGraphReference;
42+
}
43+
44+
AdviceNodeDescriptorEntityReference(EntityFetch fetchOwner, JpaGraphReference jpaGraphReference) {
45+
this.fetchOwner = fetchOwner;
46+
this.jpaGraphReference = jpaGraphReference;
47+
}
48+
49+
@Override
50+
public JpaGraphReference attributeProcessed(String attributeName) {
51+
return jpaGraphReference != null
52+
? jpaGraphReference.attributeProcessed( attributeName )
53+
: null;
54+
}
55+
56+
@Override
57+
public void applyMissingFetches() {
58+
if ( jpaGraphReference != null ) {
59+
jpaGraphReference.applyMissingFetches( fetchOwner );
60+
}
61+
}
62+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.jpa.graph.internal.advisor;
25+
26+
/** The style of advice. This is defined by the JPA spec. See tha values for details.
27+
*
28+
* @author Steve Ebersole
29+
*/
30+
public enum AdviceStyle {
31+
/**
32+
* Indicates a graph specified by the {@code javax.persistence.fetchgraph} setting.
33+
*/
34+
FETCH,
35+
/**
36+
* Indicates a graph specified by the {@code javax.persistence.loadgraph} setting.
37+
*/
38+
LOAD
39+
}

0 commit comments

Comments
 (0)