improve some error messages

This commit is contained in:
Gavin 2023-01-01 20:50:50 +01:00 committed by Gavin King
parent 90c767681e
commit c718a46285
3 changed files with 18 additions and 13 deletions

View File

@ -9,7 +9,6 @@ package org.hibernate.boot.model.internal;
import java.util.Map;
import org.hibernate.AnnotationException;
import org.hibernate.MappingException;
import org.hibernate.annotations.Check;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.ColumnTransformer;
@ -779,8 +778,9 @@ public class AnnotatedColumn {
final ColumnDefault columnDefault =
getOverridableAnnotation( property, ColumnDefault.class, getBuildingContext() );
if ( columnDefault != null ) {
if ( length!=1 ) {
throw new MappingException("@ColumnDefault may only be applied to single-column mappings");
if ( length != 1 ) {
throw new AnnotationException( "'@ColumnDefault' may only be applied to single-column mappings but '"
+ property.getName() + "' maps to " + length + " columns" );
}
setDefaultValue( columnDefault.value() );
}
@ -797,7 +797,8 @@ public class AnnotatedColumn {
getOverridableAnnotation( property, GeneratedColumn.class, getBuildingContext() );
if ( generatedColumn != null ) {
if (length!=1) {
throw new MappingException("@GeneratedColumn may only be applied to single-column mappings");
throw new AnnotationException("'@GeneratedColumn' may only be applied to single-column mappings but '"
+ property.getName() + "' maps to " + length + " columns" );
}
setGeneratedAs( generatedColumn.value() );
}
@ -812,8 +813,9 @@ public class AnnotatedColumn {
if ( property != null ) {
final Check check = getOverridableAnnotation( property, Check.class, getBuildingContext() );
if ( check != null ) {
if (length!=1) {
throw new MappingException("@Check may only be applied to single-column mappings (use a table-level @Check)");
if ( length != 1 ) {
throw new AnnotationException("'@Check' may only be applied to single-column mappings but '"
+ property.getName() + "' maps to " + length + " columns (use a table-level '@Check')" );
}
setCheckConstraint( check.constraints() );
}

View File

@ -13,10 +13,12 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.loader.internal.AliasConstantsHelper;
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.sql.Template;
import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.internal.util.StringHelper.replace;
import static org.hibernate.internal.util.StringHelper.safeInterning;
import static org.hibernate.sql.Template.TEMPLATE;
import static org.hibernate.sql.Template.renderWhereStringTemplate;
/**
* A mapping model object representing a SQL {@linkplain org.hibernate.annotations.Formula formula}
@ -42,8 +44,8 @@ public class Formula implements Selectable, Serializable {
@Override
public String getTemplate(Dialect dialect, TypeConfiguration typeConfiguration, SqmFunctionRegistry registry) {
final String template = Template.renderWhereStringTemplate( formula, dialect, typeConfiguration, registry );
return safeInterning( StringHelper.replace( template, "{alias}", Template.TEMPLATE ) );
final String template = renderWhereStringTemplate( formula, dialect, typeConfiguration, registry );
return safeInterning( replace( template, "{alias}", TEMPLATE ) );
}
@Override
@ -91,6 +93,6 @@ public class Formula implements Selectable, Serializable {
@Override
public String toString() {
return this.getClass().getSimpleName() + "( " + formula + " )";
return getClass().getSimpleName() + "( " + formula + " )";
}
}

View File

@ -15,11 +15,12 @@ import java.util.StringTokenizer;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.internal.util.StringHelper.WHITESPACE;
/**
* Parses SQL fragments specified in mapping documents.
*
@ -100,7 +101,7 @@ public final class Template {
Dialect dialect,
TypeConfiguration typeConfiguration,
SqmFunctionRegistry functionRegistry) {
return renderWhereStringTemplate(sqlWhereString, TEMPLATE, dialect, typeConfiguration, functionRegistry);
return renderWhereStringTemplate( sqlWhereString, TEMPLATE, dialect, typeConfiguration, functionRegistry );
}
/**
@ -128,7 +129,7 @@ public final class Template {
String symbols = new StringBuilder()
.append( "=><!+-*/()',|&`" )
.append( StringHelper.WHITESPACE )
.append( WHITESPACE )
.append( dialect.openQuote() )
.append( dialect.closeQuote() )
.toString();