Skip to content

Commit

Permalink
improve three error messages - don't use hbm.xml stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Feb 16, 2025
1 parent d74324d commit 52472f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class NaturalIdHelper {
public static String[] getNaturalIdPropertyNames(EntityPersister persister) {
final int[] naturalIdPropertyIndices = persister.getNaturalIdentifierProperties();
if ( naturalIdPropertyIndices == null ) {
throw new IdentifierGenerationException( "entity '" + persister.getEntityName()
throw new IdentifierGenerationException( "Entity '" + persister.getEntityName()
+ "' has no '@NaturalId' property" );
}
if ( persister.getEntityMetamodel().isNaturalIdentifierInsertGenerated() ) {
throw new IdentifierGenerationException( "entity '" + persister.getEntityName()
throw new IdentifierGenerationException( "Entity '" + persister.getEntityName()
+ "' has a '@NaturalId' property which is also defined as insert-generated" );
}
final String[] allPropertyNames = persister.getPropertyNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ else if ( !allowMutation ) {
LOG.lazyPropertyFetchingAvailable( name );
}

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

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

polymorphic = persistentClass.isPolymorphic();
inherited = persistentClass.isInherited();
superclass = inherited ?
persistentClass.getSuperclass().getEntityName() :
null;
superclass = inherited
? persistentClass.getSuperclass().getEntityName()
: null;
hasSubclasses = persistentClass.hasSubclasses();

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

hasCollections = foundCollection;
Expand Down Expand Up @@ -520,9 +526,8 @@ private void verifyNaturalIdProperty(Property property) {
final Value value = property.getValue();
if ( value instanceof ManyToOne toOne ) {
if ( toOne.getNotFoundAction() == NotFoundAction.IGNORE ) {
throw new MappingException(
"Attribute marked as natural-id can not also be a not-found association - "
+ propertyName( property )
throw new MappingException( "Association '" + propertyName( property )
+ "' marked as '@NaturalId' is also annotated '@NotFound(IGNORE)'"
);
}
}
Expand Down Expand Up @@ -585,7 +590,7 @@ private void mapPropertyToIndex(Property property, int i) {
*/
public boolean isNaturalIdentifierInsertGenerated() {
if ( naturalIdPropertyNumbers.length == 0 ) {
throw new IllegalStateException( "entity does not have a natural id: " + name );
throw new IllegalStateException( "Entity '" + name + "' does not have a natural id" );
}
for ( int i = 0; i < naturalIdPropertyNumbers.length; i++ ) {
final Generator strategy = generators[ naturalIdPropertyNumbers[i] ];
Expand Down Expand Up @@ -698,9 +703,9 @@ public NonIdentifierAttribute[] getProperties() {
}

public int getPropertyIndex(String propertyName) {
Integer index = getPropertyIndexOrNull(propertyName);
final Integer index = getPropertyIndexOrNull( propertyName );
if ( index == null ) {
throw new HibernateException("Unable to resolve property: " + propertyName);
throw new HibernateException( "Unable to resolve property: " + propertyName );
}
return index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand All @@ -41,8 +40,7 @@ void checkManyToOne(ServiceRegistryScope registryScope) {
fail( "Expecting an exception" );
}
catch (MappingException expected) {
assertThat( expected.getMessage() )
.startsWith( "Attribute marked as natural-id can not also be a not-found association - " );
// expected
}
}

Expand All @@ -57,8 +55,7 @@ void checkEmbeddable(ServiceRegistryScope registryScope) {
fail( "Expecting an exception" );
}
catch (MappingException expected) {
assertThat( expected.getMessage() )
.startsWith( "Attribute marked as natural-id can not also be a not-found association - " );
// expected
}
}

Expand Down

0 comments on commit 52472f1

Please sign in to comment.