HHH-8073 Corrected column alias creation

This commit is contained in:
Brett Meyer 2013-03-14 17:36:41 -04:00
parent a7b4e9f8fd
commit 4d20fb5334
1 changed files with 19 additions and 20 deletions

View File

@ -108,33 +108,32 @@ public class Column implements Selectable, Serializable, Cloneable {
name; name;
} }
/** @Override
* For any column name, generate an alias that is unique
* to that column name, and also 10 characters or less
* in length.
*/
public String getAlias(Dialect dialect) { public String getAlias(Dialect dialect) {
final int lastLetter = StringHelper.lastIndexOfLetter( name );
String suffix = Integer.toString(uniqueInteger) + '_';
String alias = name; String alias = name;
String unique = Integer.toString(uniqueInteger) + '_';
int lastLetter = StringHelper.lastIndexOfLetter(name);
if ( lastLetter == -1 ) { if ( lastLetter == -1 ) {
alias = "column"; alias = "column";
} }
else if ( lastLetter < name.length()-1 ) { else if ( name.length() > lastLetter + 1 ) {
alias = name.substring(0, lastLetter+1); alias = name.substring( 0, lastLetter + 1 );
} }
if ( alias.length() > dialect.getMaxAliasLength() ) {
alias = alias.substring( 0, dialect.getMaxAliasLength() - unique.length() ); boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength()
} && !quoted && !name.toLowerCase().equals( "rowid" );
boolean useRawName = name.equals(alias) && if ( !useRawName ) {
!quoted && if ( suffix.length() >= dialect.getMaxAliasLength() ) {
!name.toLowerCase().equals("rowid"); throw new MappingException( String.format(
if ( useRawName ) { "Unique suffix [%s] length must be less than maximum [%d]",
return alias; suffix, dialect.getMaxAliasLength() ) );
} }
else { if ( alias.length() + suffix.length() > dialect.getMaxAliasLength() ) {
return alias + unique; alias = alias.substring( 0, dialect.getMaxAliasLength() - suffix.length() );
}
} }
return alias + suffix;
} }
/** /**