DB2Dialect override for substring hides DB2 method in Hibernate

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7
    • 6
  • Hibernate
    • 5
    • 4
  • DB2
    • 10.5
    • 10.1
    • 9.7

Issue

  • Creating an HQL query (e.g. from Employee e where e.name = (SUBSTRING('Johnny', 1, 4, OCTETS)))
  • The DB2 driver raises com.ibm.db2.jcc.am.SqlSyntaxErrorException: ... SQLCODE=-104, SQLSTATE=42601, SQLERRMC=OCTETS ... during query execution.

Resolution

  • This is a known issue - Content from issues.jboss.org is not included.JBEAP-12906 - which will be addressed in a future release.

  • To work around this limitation in HQL queries, one of the following may be used:

        package org.hibernate.dialect;
      
        import org.hibernate.dialect.function.StandardSQLFunction;
        import org.hibernate.type.StandardBasicTypes;
    
        public class MyDB2Dialect extends DB2Dialect {
                public MyDB2Dialect() {
                        super();
                        registerFunction( "substring", new StandardSQLFunction( "substring", StandardBasicTypes.STRING ) );
                }
        }
    

References

Root Cause

The DB2Dialect has an Content from github.com is not included.override for the substring method that translates the method (when included in HQL) to substr.

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.