Skip to content

Commit caff907

Browse files
committed
throw exception for contradictory annotations
similar to what I just did for @LazyToOne, but this time for @LazyCollection
1 parent bb40606 commit caff907

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@
150150
import org.jboss.logging.Logger;
151151

152152
import static jakarta.persistence.AccessType.PROPERTY;
153+
import static jakarta.persistence.FetchType.EAGER;
154+
import static jakarta.persistence.FetchType.LAZY;
153155
import static org.hibernate.cfg.AnnotatedColumn.buildColumnFromAnnotation;
154156
import static org.hibernate.cfg.AnnotatedColumn.buildColumnFromNoAnnotation;
155157
import static org.hibernate.cfg.AnnotatedColumn.buildColumnsFromAnnotations;
@@ -1465,11 +1467,16 @@ else if ( fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT ) {
14651467
private void handleLazy(FetchType jpaFetchType) {
14661468
final LazyCollection lazy = property.getAnnotation( LazyCollection.class );
14671469
if ( lazy != null ) {
1468-
collection.setLazy( lazy.value() != LazyCollectionOption.FALSE );
1469-
collection.setExtraLazy( lazy.value() == LazyCollectionOption.EXTRA );
1470+
boolean lazyFalse = lazy.value() == LazyCollectionOption.FALSE;
1471+
if ( jpaFetchType == EAGER && !lazyFalse) {
1472+
throw new AnnotationException("Collection '" + safeCollectionRole()
1473+
+ "' is marked 'fetch=EAGER' and '@LazyCollection(" + lazy.value() + ")'");
1474+
}
1475+
collection.setLazy( !lazyFalse );
1476+
collection.setExtraLazy( lazy.value() == LazyCollectionOption.EXTRA);
14701477
}
14711478
else {
1472-
collection.setLazy( jpaFetchType == FetchType.LAZY );
1479+
collection.setLazy( jpaFetchType == LAZY );
14731480
collection.setExtraLazy( false );
14741481
}
14751482
}
@@ -1489,7 +1496,7 @@ else if ( elementCollection != null ) {
14891496
return elementCollection.fetch();
14901497
}
14911498
else if ( manyToAny != null ) {
1492-
return FetchType.LAZY;
1499+
return LAZY;
14931500
}
14941501
else {
14951502
throw new AssertionFailure(
@@ -2357,7 +2364,7 @@ private void handleManyToAny(
23572364
inverseJoinColumns,
23582365
inferredData,
23592366
cascadeDeleteEnabled,
2360-
anyAnn.fetch() == FetchType.LAZY,
2367+
anyAnn.fetch() == LAZY,
23612368
Nullability.NO_CONSTRAINT,
23622369
propertyHolder,
23632370
new EntityBinder(),

0 commit comments

Comments
 (0)