Skip to content

Commit 79073a9

Browse files
committed
HHH-8073 Corrected column alias creation
1 parent d2c4588 commit 79073a9

File tree

1 file changed

+19
-20
lines changed
  • hibernate-core/src/main/java/org/hibernate/mapping

1 file changed

+19
-20
lines changed

hibernate-core/src/main/java/org/hibernate/mapping/Column.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,32 @@ public String getQuotedName(Dialect d) {
108108
name;
109109
}
110110

111-
/**
112-
* For any column name, generate an alias that is unique
113-
* to that column name, and also 10 characters or less
114-
* in length.
115-
*/
111+
@Override
116112
public String getAlias(Dialect dialect) {
113+
final int lastLetter = StringHelper.lastIndexOfLetter( name );
114+
String suffix = Integer.toString(uniqueInteger) + '_';
115+
117116
String alias = name;
118-
String unique = Integer.toString(uniqueInteger) + '_';
119-
int lastLetter = StringHelper.lastIndexOfLetter(name);
120117
if ( lastLetter == -1 ) {
121118
alias = "column";
122119
}
123-
else if ( lastLetter < name.length()-1 ) {
124-
alias = name.substring(0, lastLetter+1);
120+
else if ( name.length() > lastLetter + 1 ) {
121+
alias = name.substring( 0, lastLetter + 1 );
125122
}
126-
if ( alias.length() > dialect.getMaxAliasLength() ) {
127-
alias = alias.substring( 0, dialect.getMaxAliasLength() - unique.length() );
128-
}
129-
boolean useRawName = name.equals(alias) &&
130-
!quoted &&
131-
!name.toLowerCase().equals("rowid");
132-
if ( useRawName ) {
133-
return alias;
134-
}
135-
else {
136-
return alias + unique;
123+
124+
boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength()
125+
&& !quoted && !name.toLowerCase().equals( "rowid" );
126+
if ( !useRawName ) {
127+
if ( suffix.length() >= dialect.getMaxAliasLength() ) {
128+
throw new MappingException( String.format(
129+
"Unique suffix [%s] length must be less than maximum [%d]",
130+
suffix, dialect.getMaxAliasLength() ) );
131+
}
132+
if ( alias.length() + suffix.length() > dialect.getMaxAliasLength() ) {
133+
alias = alias.substring( 0, dialect.getMaxAliasLength() - suffix.length() );
134+
}
137135
}
136+
return alias + suffix;
138137
}
139138

140139
/**

0 commit comments

Comments
 (0)