Skip to content

Commit 454af9c

Browse files
committed
error when a @naturalid field is LAZY
1 parent 724212b commit 454af9c

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,18 @@ private void handleNaturalId(Property property) {
508508
+ "' belongs to an entity subclass and may not be annotated '@NaturalId'" +
509509
" (only a property of a root '@Entity' or a '@MappedSuperclass' may be a '@NaturalId')" );
510510
}
511+
if ( memberDetails.hasDirectAnnotationUsage( ManyToOne.class ) ) {
512+
if ( memberDetails.getDirectAnnotationUsage( ManyToOne.class ).fetch() == LAZY ) {
513+
throw new AnnotationException( "Property '" + qualify( holder.getPath(), name )
514+
+ " is annotated '@NaturalId' and '@ManyToOne(fetch=LAZY)' (natural id fields may not be lazy)" );
515+
}
516+
}
517+
if ( memberDetails.hasDirectAnnotationUsage( Basic.class ) ) {
518+
if ( memberDetails.getDirectAnnotationUsage( Basic.class ).fetch() == LAZY ) {
519+
throw new AnnotationException( "Property '" + qualify( holder.getPath(), name )
520+
+ " is annotated '@NaturalId' and '@Basic(fetch=LAZY)' (natural id fields may not be lazy)" );
521+
}
522+
}
511523
if ( !naturalId.mutable() ) {
512524
updatable = false;
513525
}

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,11 +2022,7 @@ private Property createManyToOneAttribute(
20222022

20232023
Property prop = new Property();
20242024
prop.setValue( manyToOneBinding );
2025-
bindProperty(
2026-
sourceDocument,
2027-
manyToOneSource,
2028-
prop
2029-
);
2025+
bindProperty( sourceDocument, manyToOneSource, prop );
20302026

20312027
if ( isNotEmpty( manyToOneSource.getCascadeStyleName() ) ) {
20322028
// todo : would be better to delay this the end of binding (second pass, etc)

0 commit comments

Comments
 (0)