user1396576
user1396576

Reputation: 7561

Hibernate HQL aliases

I'd like to know if for a given HQL query, the generated SQL query will always be the same or if it can vary, especially when it comes to aliases. I'm asking this question because our DBA complains about the fact that for a given query, the generated aliases can vary from one run of the application to another. Thanks for your help.

Regards, Nicolas

Upvotes: 1

Views: 1758

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153730

Hibernate has to ensure unique aliases, because you can self join a table as well, and according to Hibernate source code, the org.hibernate.mapping.Column.getAlias() method doesn't allow you to override the default unique alias strategy:

public String getAlias(Dialect dialect) {
    final int lastLetter = StringHelper.lastIndexOfLetter( name );
    final String suffix = Integer.toString(uniqueInteger) + '_';

    String alias = name;
    if ( lastLetter == -1 ) {
        alias = "column";
    }
    else if ( name.length() > lastLetter + 1 ) {
        alias = name.substring( 0, lastLetter + 1 );
    }

    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;
}

So you are stuck with those unique names.

Upvotes: 1

Related Questions