@@ -115,16 +115,16 @@ public string GetQuotedName(Dialect.Dialect d)
115
115
return IsQuoted ? d . QuoteForColumnName ( _name ) : _name ;
116
116
}
117
117
118
- /**
119
- * For any column name, generate an alias that is unique
120
- * to that column name, and also 10 characters or less
121
- * in length.
122
- */
123
118
119
+ /// <summary>
120
+ /// For any column name, generate an alias that is unique to that
121
+ /// column name, and also take Dialect.MaxAliasLength into account.
122
+ /// </summary>
124
123
public string GetAlias ( Dialect . Dialect dialect )
125
124
{
126
125
string alias = _name ;
127
- string suffix = UniqueInteger . ToString ( ) + '_' ;
126
+ string suffix = UniqueInteger . ToString ( ) + StringHelper . Underscore ;
127
+
128
128
int lastLetter = StringHelper . LastIndexOfLetter ( _name ) ;
129
129
if ( lastLetter == - 1 )
130
130
{
@@ -134,22 +134,31 @@ public string GetAlias(Dialect.Dialect dialect)
134
134
{
135
135
alias = _name . Substring ( 0 , lastLetter + 1 ) ;
136
136
}
137
- if ( alias . Length > dialect . MaxAliasLength )
138
- {
139
- alias = alias . Substring ( 0 , dialect . MaxAliasLength - suffix . Length ) ;
140
- }
141
- bool useRawName = _name . Equals ( alias ) &&
142
- ! _quoted &&
143
- ! StringHelper . EqualsCaseInsensitive ( _name , "rowid" ) ;
144
137
145
- if ( useRawName )
138
+ // Updated logic ported from Hibernate's fix for HHH-8073.
139
+ // https://github.com/hibernate/hibernate-orm/commit/79073a98f0e4ed225fe4608b67594196f86d48d7
140
+ // To my mind it is weird - since the suffix is now always used, it
141
+ // seems "useRawName" is a misleading choice of variable name. For the same
142
+ // reason, the checks for "_quoted" and "rowid" looks redundant. If you remove
143
+ // those checks, then the double checks for total length can be reduced to one.
144
+ // But I will leave it like this for now to make it look similar. /Oskar 2016-08-20
145
+ bool useRawName = _name . Length + suffix . Length <= dialect . MaxAliasLength &&
146
+ ! _quoted &&
147
+ ! StringHelper . EqualsCaseInsensitive ( _name , "rowid" ) ;
148
+ if ( ! useRawName )
146
149
{
147
- return alias ;
148
- }
149
- else
150
- {
151
- return alias + suffix ;
150
+ if ( suffix . Length >= dialect . MaxAliasLength )
151
+ {
152
+ throw new MappingException (
153
+ string . Format (
154
+ "Unique suffix {0} length must be less than maximum {1} characters." ,
155
+ suffix ,
156
+ dialect . MaxAliasLength ) ) ;
157
+ }
158
+ if ( alias . Length + suffix . Length > dialect . MaxAliasLength )
159
+ alias = alias . Substring ( 0 , dialect . MaxAliasLength - suffix . Length ) ;
152
160
}
161
+ return alias + suffix ;
153
162
}
154
163
155
164
public string GetAlias ( Dialect . Dialect dialect , Table table )
0 commit comments