@@ -115,16 +115,21 @@ public string GetQuotedName(Dialect.Dialect d)
115115 return IsQuoted ? d . QuoteForColumnName ( _name ) : _name ;
116116 }
117117
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- */
123118
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>
124123 public string GetAlias ( Dialect . Dialect dialect )
124+ {
125+ return GetAlias ( dialect . MaxAliasLength ) ;
126+ }
127+
128+ private string GetAlias ( int maxAliasLength )
125129 {
126130 string alias = _name ;
127- string suffix = UniqueInteger . ToString ( ) + '_' ;
131+ string suffix = UniqueInteger . ToString ( ) + StringHelper . Underscore ;
132+
128133 int lastLetter = StringHelper . LastIndexOfLetter ( _name ) ;
129134 if ( lastLetter == - 1 )
130135 {
@@ -134,27 +139,38 @@ public string GetAlias(Dialect.Dialect dialect)
134139 {
135140 alias = _name . Substring ( 0 , lastLetter + 1 ) ;
136141 }
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" ) ;
144142
145- if ( useRawName )
146- {
147- return alias ;
148- }
149- else
143+ // Updated logic ported from Hibernate's fix for HHH-8073.
144+ // https://github.com/hibernate/hibernate-orm/commit/79073a98f0e4ed225fe4608b67594196f86d48d7
145+ // To my mind it is weird - since the suffix is now always used, it
146+ // seems "useRawName" is a misleading choice of variable name. For the same
147+ // reason, the checks for "_quoted" and "rowid" looks redundant. If you remove
148+ // those checks, then the double checks for total length can be reduced to one.
149+ // But I will leave it like this for now to make it look similar. /Oskar 2016-08-20
150+ bool useRawName = _name . Length + suffix . Length <= maxAliasLength &&
151+ ! _quoted &&
152+ ! StringHelper . EqualsCaseInsensitive ( _name , "rowid" ) ;
153+ if ( ! useRawName )
150154 {
151- return alias + suffix ;
155+ if ( suffix . Length >= maxAliasLength )
156+ {
157+ throw new MappingException (
158+ string . Format (
159+ "Unique suffix {0} length must be less than maximum {1} characters." ,
160+ suffix ,
161+ maxAliasLength ) ) ;
162+ }
163+ if ( alias . Length + suffix . Length > maxAliasLength )
164+ alias = alias . Substring ( 0 , maxAliasLength - suffix . Length ) ;
152165 }
166+ return alias + suffix ;
153167 }
154168
155169 public string GetAlias ( Dialect . Dialect dialect , Table table )
156170 {
157- return GetAlias ( dialect ) + table . UniqueInteger + StringHelper . Underscore ;
171+ string suffix = table . UniqueInteger . ToString ( ) + StringHelper . Underscore ;
172+ int maxAliasLength = dialect . MaxAliasLength - suffix . Length ;
173+ return GetAlias ( maxAliasLength ) + suffix ;
158174 }
159175
160176
0 commit comments