property names beginning with underscores cause Hibernate to generate invalid aliases


Hibernate is generating invalid SQL (for Oracle) for collections that are stored in fields with names that start with underscores.

In this SQL:
Hibernate: select tags0.SIMPLE_OBJECT_ID as SIMPLE1_0_, tags0.TAG as TAG0_ from SIMPLE_OBJECT_TAG tags0 where tags0.SIMPLE_OBJECT_ID=?

an alias called "tags0" is created for the collection table. Unfortunately, Oracle does not allow non-alphabetic characters for the first character in an alias.

Here's the stacktrace:
Hibernate: select tags0.SIMPLE_OBJECT_ID as SIMPLE1_0_, tags0.TAG as TAG0_ from SIMPLE_OBJECT_TAG tags0 where tags0.SIMPLE_OBJECT_ID=?
WARN JDBCExceptionReporter 20060822-011120.817 - SQL Error: 911, SQLState: 42000
ERROR JDBCExceptionReporter 20060822-011120.818 - ORA-00911: invalid character

INFO DefaultLoadEventListener 20060822-011120.820 - Error performing load command
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [SimpleObject._tags#1]
at org.hibernate.exception.SQLStateConverter.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.loader.Loader.loadCollection(
at org.hibernate.loader.collection.CollectionLoader.initialize(
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(
at org.hibernate.impl.SessionImpl.initializeCollection(
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(
at org.hibernate.loader.Loader.loadEntity(
at org.hibernate.loader.entity.AbstractEntityLoader.load(
at org.hibernate.loader.entity.AbstractEntityLoader.load(
at org.hibernate.persister.entity.AbstractEntityPersister.load(
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(
at org.hibernate.event.def.DefaultLoadEventListener.load(
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(
at org.hibernate.impl.SessionImpl.fireLoad(
at org.hibernate.impl.SessionImpl.get(
at org.hibernate.impl.SessionImpl.get(
at InvalidCharacterErrorCase.runTest(
at InvalidCharacterErrorCase.main(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.intellij.rt.execution.application.AppMain.main(
Caused by: java.sql.SQLException: ORA-00911: invalid character

at oracle.jdbc.driver.DatabaseError.throwSqlException(
at oracle.jdbc.driver.T4CTTIoer.processError(
at oracle.jdbc.driver.T4CTTIoer.processError(
at oracle.jdbc.driver.T4C8Oall.receive(
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(
at org.hibernate.jdbc.AbstractBatcher.getResultSet(
at org.hibernate.loader.Loader.getResultSet(
at org.hibernate.loader.Loader.doQuery(
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(
at org.hibernate.loader.Loader.loadCollection(
... 26 more

I have attached a test case that causes this error.

After a little bit of research, it appears that a change to StringHelper.generateAlias(String description, int unique) (or StringHelper.generateAliasRoot(String description)) to remove all characters up to the first alphabetic character would solve this problem.


Hibernate 3.1.3


Jerry Cattell
August 22, 2006, 8:56 AM

I am attaching a possible patch to org.hibernate.util.StringHelper that solves the problem.

Jerry Cattell
August 22, 2006, 8:59 AM

I should probably mention that the patch was generated against version 3.1.3.

Steve Ebersole
August 23, 2006, 3:39 PM


Matej Sekoranja
September 5, 2007, 12:49 PM

I am using Hibernate 3.2.5 and Oracle 10g Express Edition.
I got the same error as reported and "fixed" in this report.
This fix works (and implementation) only if a alias contains at least one alphabetic character. If not, the same alias is returned as given (and not the cleaned one), e.g. "0.

My fix of method:


  • Clean the generated alias by removing any non-alpha characters from the
    * beginning.
    * @param alias The generated alias to be cleaned.
    * @return The cleaned alias, stripped of any leading non-alpha characters.
    private static String cleanAlias(String alias) {
    char[] chars = alias.toCharArray();
    // short cut check...
    if ( !Character.isLetter( chars[0] ) ) {
    for ( int i = 1; i < chars.length; i++ ) {
    // as soon as we encounter our first letter, return the substring
    // from that position
    if ( Character.isLetter( chars[i] ) ) {
    return alias.substring( i );
    + // no alphabetic characters in alias, prefix with some
    + return "fix" + alias;
    return alias;

Matej Sekoranja
April 9, 2013, 9:32 PM

This is still an issue.


Steve Ebersole


Jerry Cattell

Fix versions





Suitable for new contributors


Requires Release Note


Pull Request





Affects versions