Question

I have some code that does some date logic, and wanted to create custom Hibernate dialects for the various databases we may need to use with our application so that the database queries will work with all databases just by setting the dialect.

For MySQL:

public class ExtendedMySQLDialect extends MySQLDialect {

public ExtendedMySQLDialect() {
    super();
    SQLFunctionTemplate dateSubDays = new SQLFunctionTemplate(Hibernate.DATE, "DATE_SUB(SYSDATE(),INTERVAL ?1 DAY)");
    registerFunction("date_sub_days", dateSubDays);

    SQLFunctionTemplate dateSubDaysGreatestFunction = new SQLFunctionTemplate(Hibernate.DATE, "DATE_SUB(SYSDATE(),INTERVAL GREATEST(?1,?2,?3) DAY)");
    registerFunction("date_sub_days_greatest", dateSubDaysGreatestFunction);
}
}

So far so good, in a standalone application I can run this fine by changing the dialect to ExtendedMySQLDialect, and making sure the translator factory is 'ASTQueryTranslatorFactory' (it doesn't seem to work with the legacy one which I believe is called ClassicQueryTranslatorFactor or something like that - you get an error saying 'Function xxx not found').

This is where it gets weird. Using the exact same hibernate config file, same version of hibernate, same version of antlr, and same version of the dialect, deplyed under axis2 in Tomcat I get the 'FUNCTION mydb.date_sub_days does not exist' error that I got the first time with the ClassicQueryTranslatorFactory. But I can see in the startup log for Tomcat:

[INFO] RDBMS: MySQL, version: 5.6.11
[INFO] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
[INFO] Using dialect: com.capxd.infra.model.dialects.ExtendedMySQLDialect
[INFO] Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
[INFO] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
[INFO] Automatic flush during beforeCompletion(): disabled
[INFO] Automatic session close at end of transaction: disabled
[INFO] JDBC batch size: 15
[INFO] JDBC batch updates for versioned data: disabled
[INFO] Scrollable result sets: enabled
[INFO] JDBC3 getGeneratedKeys(): enabled
[INFO] Connection release mode: auto
[INFO] Default schema: mydb
[INFO] Maximum outer join fetch depth: 2
[INFO] Default batch fetch size: 1
[INFO] Generate SQL with comments: disabled
[INFO] Order SQL updates by primary key: disabled
[INFO] Order SQL inserts for batching: disabled
->> [INFO] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[INFO] Using ASTQueryTranslatorFactory
[INFO] Query language substitutions: {}
[INFO] JPA-QL strict compliance: disabled
[INFO] Second-level cache: enabled
[INFO] Query cache: disabled
[INFO] Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
[INFO] Optimize cache for minimal puts: disabled
[INFO] Structured second-level cache entries: disabled
[INFO] Statistics: disabled
[INFO] Deleted entity synthetic identifier rollback: disabled
[INFO] Default entity-mode: pojo
[INFO] Named query checking : enabled
[INFO] Hibernate Search 3.1.0.GA
[INFO] Hibernate Commons Annotations 3.1.0.GA
[INFO] building session factory
[INFO] Not binding factory to JNDI, no JNDI name configured

Any suggestions as to why this could be missing my Function? I have checked the lib folder of axis2 and it had antlr-2.7.7 in it, so I took it out (my standalone app is using 2.7.6) but didn't seem to make any difference. Anything I can check to see why my Function is not being found? Is there a way to print out whether Hibernate has registered my function or not?

EDIT!!

Turns out, I was confusing two separate queries. One is a 'query':

<query name="recent_orders">

And the other is a 'sql-query':

<sql-query name="delete_orders">

Changing the sql-query into a query allowed the Translator to pick it up and now it works...

Was it helpful?

Solution

The translator does not work with named queries labelled as a 'sql-query', only with those labelled as 'query'. The syntax is slightly different but can use custom dialect functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top