Skip to content

Commit 52472f1

Browse files
committed
improve three error messages - don't use hbm.xml stuff
1 parent d74324d commit 52472f1

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

hibernate-core/src/main/java/org/hibernate/generator/internal/NaturalIdHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public class NaturalIdHelper {
1414
public static String[] getNaturalIdPropertyNames(EntityPersister persister) {
1515
final int[] naturalIdPropertyIndices = persister.getNaturalIdentifierProperties();
1616
if ( naturalIdPropertyIndices == null ) {
17-
throw new IdentifierGenerationException( "entity '" + persister.getEntityName()
17+
throw new IdentifierGenerationException( "Entity '" + persister.getEntityName()
1818
+ "' has no '@NaturalId' property" );
1919
}
2020
if ( persister.getEntityMetamodel().isNaturalIdentifierInsertGenerated() ) {
21-
throw new IdentifierGenerationException( "entity '" + persister.getEntityName()
21+
throw new IdentifierGenerationException( "Entity '" + persister.getEntityName()
2222
+ "' has a '@NaturalId' property which is also defined as insert-generated" );
2323
}
2424
final String[] allPropertyNames = persister.getPropertyNames();

hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,10 @@ else if ( !allowMutation ) {
422422
LOG.lazyPropertyFetchingAvailable( name );
423423
}
424424

425-
lazy = persistentClass.isLazy() && (
425+
lazy = persistentClass.isLazy()
426426
// TODO: this disables laziness even in non-pojo entity modes:
427-
!persistentClass.hasPojoRepresentation() || !isFinalClass( persistentClass.getProxyInterface() ) )
428-
|| bytecodeEnhancementMetadata.isEnhancedForLazyLoading();
427+
&& (!persistentClass.hasPojoRepresentation() || !isFinalClass( persistentClass.getProxyInterface() ) )
428+
|| bytecodeEnhancementMetadata.isEnhancedForLazyLoading();
429429

430430
mutable = persistentClass.isMutable();
431431
if ( persistentClass.isAbstract() == null ) {
@@ -451,18 +451,24 @@ && isAbstractClass( persistentClass.getMappedClass() ) ) {
451451

452452
polymorphic = persistentClass.isPolymorphic();
453453
inherited = persistentClass.isInherited();
454-
superclass = inherited ?
455-
persistentClass.getSuperclass().getEntityName() :
456-
null;
454+
superclass = inherited
455+
? persistentClass.getSuperclass().getEntityName()
456+
: null;
457457
hasSubclasses = persistentClass.hasSubclasses();
458458

459459
optimisticLockStyle = persistentClass.getOptimisticLockStyle();
460-
final boolean isAllOrDirty = optimisticLockStyle.isAllOrDirty();
461-
if ( isAllOrDirty && !dynamicUpdate ) {
462-
throw new MappingException( "optimistic-lock=all|dirty requires dynamic-update=\"true\": " + name );
463-
}
464-
if ( versionPropertyIndex != NO_VERSION_INDX && isAllOrDirty ) {
465-
throw new MappingException( "version and optimistic-lock=all|dirty are not a valid combination : " + name );
460+
//TODO: move these checks into the Binders
461+
if ( optimisticLockStyle.isAllOrDirty() ) {
462+
if ( !dynamicUpdate ) {
463+
throw new MappingException( "Entity '" + name
464+
+ "' has 'OptimisticLockType." + optimisticLockStyle
465+
+ "' but is not annotated '@DynamicUpdate'" );
466+
}
467+
if ( versionPropertyIndex != NO_VERSION_INDX ) {
468+
throw new MappingException( "Entity '" + name
469+
+ "' has 'OptimisticLockType." + optimisticLockStyle
470+
+ "' but declares a '@Version' field" );
471+
}
466472
}
467473

468474
hasCollections = foundCollection;
@@ -520,9 +526,8 @@ private void verifyNaturalIdProperty(Property property) {
520526
final Value value = property.getValue();
521527
if ( value instanceof ManyToOne toOne ) {
522528
if ( toOne.getNotFoundAction() == NotFoundAction.IGNORE ) {
523-
throw new MappingException(
524-
"Attribute marked as natural-id can not also be a not-found association - "
525-
+ propertyName( property )
529+
throw new MappingException( "Association '" + propertyName( property )
530+
+ "' marked as '@NaturalId' is also annotated '@NotFound(IGNORE)'"
526531
);
527532
}
528533
}
@@ -585,7 +590,7 @@ private void mapPropertyToIndex(Property property, int i) {
585590
*/
586591
public boolean isNaturalIdentifierInsertGenerated() {
587592
if ( naturalIdPropertyNumbers.length == 0 ) {
588-
throw new IllegalStateException( "entity does not have a natural id: " + name );
593+
throw new IllegalStateException( "Entity '" + name + "' does not have a natural id" );
589594
}
590595
for ( int i = 0; i < naturalIdPropertyNumbers.length; i++ ) {
591596
final Generator strategy = generators[ naturalIdPropertyNumbers[i] ];
@@ -698,9 +703,9 @@ public NonIdentifierAttribute[] getProperties() {
698703
}
699704

700705
public int getPropertyIndex(String propertyName) {
701-
Integer index = getPropertyIndexOrNull(propertyName);
706+
final Integer index = getPropertyIndexOrNull( propertyName );
702707
if ( index == null ) {
703-
throw new HibernateException("Unable to resolve property: " + propertyName);
708+
throw new HibernateException( "Unable to resolve property: " + propertyName );
704709
}
705710
return index;
706711
}

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/naturalid/ValidationTests.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import jakarta.persistence.ManyToOne;
2424
import jakarta.persistence.Table;
2525

26-
import static org.assertj.core.api.Assertions.assertThat;
2726
import static org.junit.jupiter.api.Assertions.fail;
2827

2928
/**
@@ -41,8 +40,7 @@ void checkManyToOne(ServiceRegistryScope registryScope) {
4140
fail( "Expecting an exception" );
4241
}
4342
catch (MappingException expected) {
44-
assertThat( expected.getMessage() )
45-
.startsWith( "Attribute marked as natural-id can not also be a not-found association - " );
43+
// expected
4644
}
4745
}
4846

@@ -57,8 +55,7 @@ void checkEmbeddable(ServiceRegistryScope registryScope) {
5755
fail( "Expecting an exception" );
5856
}
5957
catch (MappingException expected) {
60-
assertThat( expected.getMessage() )
61-
.startsWith( "Attribute marked as natural-id can not also be a not-found association - " );
58+
// expected
6259
}
6360
}
6461

0 commit comments

Comments
 (0)