Skip to content

Commit e6f8313

Browse files
committed
suggested fix for @fetch(JOIN) problem
1 parent 94c3692 commit e6f8313

File tree

4 files changed

+28
-44
lines changed

4 files changed

+28
-44
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,10 @@ private void setHibernateFetchMode(org.hibernate.annotations.FetchMode fetchMode
15121512
switch ( fetchMode ) {
15131513
case JOIN:
15141514
collection.setFetchMode( FetchMode.JOIN );
1515-
collection.setLazy( false );
1515+
if ( collection.isLazy() ) {
1516+
throw new AnnotationException("Collection '" + safeCollectionRole()
1517+
+ "' is mapped 'fetch=LAZY' and '@Fetch(JOIN)' (join fetching is always eager)");
1518+
}
15161519
break;
15171520
case SELECT:
15181521
collection.setFetchMode( FetchMode.SELECT );

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,11 @@ private static void setHibernateFetchMode(ToOne toOne, XProperty property, org.h
395395
switch ( fetchMode ) {
396396
case JOIN:
397397
toOne.setFetchMode( FetchMode.JOIN );
398-
toOne.setLazy( false );
399398
toOne.setUnwrapProxy( false );
399+
if ( toOne.isLazy() ) {
400+
throw new AnnotationException("Association '" + property.getName()
401+
+ "' is mapped 'fetch=LAZY' and '@Fetch(JOIN)' (join fetching is always eager)");
402+
}
400403
break;
401404
case SELECT:
402405
toOne.setFetchMode( FetchMode.SELECT );

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/FetchOptionsHelper.java

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,24 @@
66
*/
77
package org.hibernate.metamodel.mapping.internal;
88

9+
import org.hibernate.AssertionFailure;
910
import org.hibernate.FetchMode;
1011
import org.hibernate.engine.FetchStyle;
1112
import org.hibernate.engine.FetchTiming;
1213
import org.hibernate.engine.spi.SessionFactoryImplementor;
13-
import org.hibernate.internal.CoreLogging;
14-
import org.hibernate.internal.CoreMessageLogger;
1514
import org.hibernate.persister.collection.AbstractCollectionPersister;
1615
import org.hibernate.persister.collection.CollectionPersister;
1716
import org.hibernate.persister.entity.EntityPersister;
1817
import org.hibernate.sql.results.graph.FetchOptions;
1918
import org.hibernate.type.AssociationType;
2019

20+
import static org.hibernate.engine.FetchStyle.JOIN;
21+
2122
/**
2223
* @author Steve Ebersole
2324
*/
2425
public final class FetchOptionsHelper {
2526

26-
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( FetchOptionsHelper.class );
27-
2827
private FetchOptionsHelper() {
2928
}
3029

@@ -45,7 +44,7 @@ public static FetchStyle determineFetchStyleByMetadata(
4544
}
4645

4746
if ( mappingFetchMode == FetchMode.JOIN ) {
48-
return FetchStyle.JOIN;
47+
return JOIN;
4948
}
5049

5150
if ( type.isEntityType() ) {
@@ -57,7 +56,7 @@ else if ( mappingFetchMode == FetchMode.SELECT ) {
5756
return FetchStyle.SELECT;
5857
}
5958
else if ( !persister.hasProxy() ) {
60-
return FetchStyle.JOIN;
59+
return JOIN;
6160
}
6261
}
6362
else {
@@ -77,41 +76,24 @@ public static FetchTiming determineFetchTiming(
7776
FetchStyle style,
7877
AssociationType type,
7978
SessionFactoryImplementor sessionFactory) {
80-
switch ( style ) {
81-
case JOIN: {
82-
return FetchTiming.IMMEDIATE;
83-
}
84-
case BATCH:
85-
case SUBSELECT:
86-
default: {
87-
return isSubsequentSelectDelayed( type, sessionFactory )
88-
? FetchTiming.DELAYED
89-
: FetchTiming.IMMEDIATE;
90-
}
91-
}
79+
return determineFetchTiming( style, type, false, sessionFactory );
9280
}
9381

9482
public static FetchTiming determineFetchTiming(
9583
FetchStyle style,
9684
AssociationType type,
9785
boolean lazy,
98-
String role,
9986
SessionFactoryImplementor sessionFactory) {
100-
switch ( style ) {
101-
case JOIN: {
102-
if ( lazy ) {
103-
LOG.fetchModeJoinWithLazyWarning( role );
104-
return FetchTiming.DELAYED;
105-
}
106-
return FetchTiming.IMMEDIATE;
107-
}
108-
case BATCH:
109-
case SUBSELECT:
110-
default: {
111-
return isSubsequentSelectDelayed( type, sessionFactory )
112-
? FetchTiming.DELAYED
113-
: FetchTiming.IMMEDIATE;
87+
if ( style == JOIN ) {
88+
if ( lazy ) {
89+
throw new AssertionFailure("JOIN FetchStyle with LAZY fetching");
11490
}
91+
return FetchTiming.IMMEDIATE;
92+
}
93+
else {
94+
return isSubsequentSelectDelayed( type, sessionFactory )
95+
? FetchTiming.DELAYED
96+
: FetchTiming.IMMEDIATE;
11597
}
11698
}
11799

@@ -132,6 +114,6 @@ else if ( type.isEntityType() ) {
132114

133115
public static boolean isJoinFetched(FetchOptions fetchOptions) {
134116
return fetchOptions.getTiming() == FetchTiming.IMMEDIATE
135-
&& fetchOptions.getStyle() == FetchStyle.JOIN;
117+
&& fetchOptions.getStyle() == JOIN;
136118
}
137119
}

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,10 @@ public Object assemble(Serializable cached, SharedSessionContract session) {
425425
}
426426
}
427427

428-
@SuppressWarnings("rawtypes")
429428
public static AttributeMetadata getAttributeMetadata(PropertyAccess propertyAccess) {
430429
return new SimpleAttributeMetadata( propertyAccess, ImmutableMutabilityPlan.INSTANCE, false, true, false, false, true, null);// todo (6.0) : not sure if CascadeStyle=null is correct
431430
}
432431

433-
@SuppressWarnings("rawtypes")
434432
public static PluralAttributeMapping buildPluralAttributeMapping(
435433
String attrName,
436434
int stateArrayPosition,
@@ -662,7 +660,6 @@ public static PluralAttributeMapping buildPluralAttributeMapping(
662660
style,
663661
collectionDescriptor.getCollectionType(),
664662
collectionDescriptor.isLazy(),
665-
collectionDescriptor.getRole(),
666663
sessionFactory
667664
);
668665

@@ -1342,7 +1339,7 @@ private static CollectionPart interpretMapKey(
13421339

13431340
if ( bootMapKeyDescriptor instanceof Component ) {
13441341
final Component component = (Component) bootMapKeyDescriptor;
1345-
final CompositeType compositeType = (CompositeType) component.getType();
1342+
final CompositeType compositeType = component.getType();
13461343

13471344

13481345
final EmbeddableMappingTypeImpl mappingType = EmbeddableMappingTypeImpl.from(
@@ -1704,17 +1701,16 @@ public static ToOneAttributeMapping buildSingularAssociationAttributeMapping(
17041701
);
17051702

17061703
final FetchTiming fetchTiming;
1707-
final String role = declaringType.getNavigableRole().toString() + "." + bootProperty.getName();
17081704
final boolean lazy = value.isLazy();
17091705
if ( lazy && entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {
17101706
if ( value.isUnwrapProxy() ) {
1711-
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, lazy, role, sessionFactory );
1707+
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, lazy, sessionFactory );
17121708
}
17131709
else if ( value instanceof ManyToOne && value.isNullable() && ( (ManyToOne) value ).isIgnoreNotFound() ) {
17141710
fetchTiming = FetchTiming.IMMEDIATE;
17151711
}
17161712
else {
1717-
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, lazy, role, sessionFactory );
1713+
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, lazy, sessionFactory );
17181714
}
17191715
}
17201716
else if ( !lazy
@@ -1732,7 +1728,7 @@ else if ( !lazy
17321728
}
17331729
}
17341730
else {
1735-
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, lazy, role, sessionFactory );
1731+
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, lazy, sessionFactory );
17361732
}
17371733

17381734
final ToOneAttributeMapping attributeMapping = mappingConverter.apply( new ToOneAttributeMapping(

0 commit comments

Comments
 (0)