From 4d20fb5334b0eef960448416a9c0a0335097a281 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Thu, 14 Mar 2013 17:36:41 -0400 Subject: [PATCH] HHH-8073 Corrected column alias creation --- .../java/org/hibernate/mapping/Column.java | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Column.java b/hibernate-core/src/main/java/org/hibernate/mapping/Column.java index 39ec40ecfb..2c0dccc331 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Column.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Column.java @@ -108,33 +108,32 @@ public class Column implements Selectable, Serializable, Cloneable { name; } - /** - * For any column name, generate an alias that is unique - * to that column name, and also 10 characters or less - * in length. - */ + @Override public String getAlias(Dialect dialect) { + final int lastLetter = StringHelper.lastIndexOfLetter( name ); + String suffix = Integer.toString(uniqueInteger) + '_'; + String alias = name; - String unique = Integer.toString(uniqueInteger) + '_'; - int lastLetter = StringHelper.lastIndexOfLetter(name); if ( lastLetter == -1 ) { alias = "column"; } - else if ( lastLetter < name.length()-1 ) { - alias = name.substring(0, lastLetter+1); + else if ( name.length() > lastLetter + 1 ) { + alias = name.substring( 0, lastLetter + 1 ); } - if ( alias.length() > dialect.getMaxAliasLength() ) { - alias = alias.substring( 0, dialect.getMaxAliasLength() - unique.length() ); - } - boolean useRawName = name.equals(alias) && - !quoted && - !name.toLowerCase().equals("rowid"); - if ( useRawName ) { - return alias; - } - else { - return alias + unique; + + boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength() + && !quoted && !name.toLowerCase().equals( "rowid" ); + if ( !useRawName ) { + if ( suffix.length() >= dialect.getMaxAliasLength() ) { + throw new MappingException( String.format( + "Unique suffix [%s] length must be less than maximum [%d]", + suffix, dialect.getMaxAliasLength() ) ); + } + if ( alias.length() + suffix.length() > dialect.getMaxAliasLength() ) { + alias = alias.substring( 0, dialect.getMaxAliasLength() - suffix.length() ); + } } + return alias + suffix; } /**