I have a problem with a custom SQLFunction and MySQLDialect which has only started happening since switching from Hibernate 3.0 to Hibernate 3.0.2 and 3.0.3.
I have written a MySQLBitwiseAndSQLFunction class derived from StandardSQLFunction which implements the bitwise AND functionality for the MySQLDialect which used to work in Hibernate 2 but was dropped from Hibernate 3.
Hibernate version:
3.0.2 and 3.0.3
Mapping documents:
Not relevent
Code between sessionFactory.openSession() and session.close():
My SQLFunction class looks like the following:
Code:
public class MySQLBitwiseAndSQLFunction extends StandardSQLFunction {
public MySQLBitwiseAndSQLFunction(String name) {
super(name);
}
public MySQLBitwiseAndSQLFunction(String name, Type typeValue) {
super(name, typeValue);
}
public String render(List args) {
if (args.size() != 2) {
throw new IllegalArgumentException("MySQLBitwiseAndSQLFunction requires 2 arguments!");
}
StringBuffer buffer = new StringBuffer(args.get(0).toString());
buffer.append(" & ").append(args.get(1));
return buffer.toString();
}
}
and my MySQLDialect class looks like this:
Code:
public class MySQLDialect extends org.hibernate.dialect.MySQLDialect {
public MySQLDialect() {
super();
logger.info("Creating MySQLDialect instance");
registerFunction("bitwise_and", new MySQLBitwiseAndSQLFunction("bitwise_and", Hibernate.INTEGER));
}
}
My application is configured to use this custom dialect using the hibernate.dialect property.
Full stack trace of any exception that occurs:Not applicable
Name and version of the database you are using:RDBMS: MySQL, version: 4.0.12-nt
JDBC driver: MySQL-AB JDBC Driver, version: 3.0.7-stable ( $Date: 2003/04/08 18:29:14 $, $Revision: 1.27.2.16 $ )
The generated SQL (show_sql=true):See below
Debug level Hibernate log excerpt:See below
Under Hibernate 3.0 my custom SQLFunction works entirely as expected - "bitwise_and(X,Y)" references in HQL queries gets mapped to "X & Y". However, under Hibernate 3.0.2 and 3.0.3 (sorry, haven't tried 3.0.1) I get exceptions when executing the SQL because the expected bitwise_and function translation has not taken place.
Looking at log output, under Hibernate 3.0 you can see that my custom SQLFunction class is called and the expected HQL -> SQL translation occurs:
Code:
2005-05-12 18:31:09,992 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.ErrorCounter] > throwQueryException() : no errors
2005-05-12 18:31:10,002 [Request Handler 1] [INFO ] [2] [com.purplegem.persist.MySQLBitwiseAndSQLFunction] > Rendering bitwise_and function to SQL
2005-05-12 18:31:10,002 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.QueryTranslatorImpl] > HQL: select count(*) from com.purplegem.domain.supplier.Supplier supplier where supplier.shortName like :shortName and supplier.address.town like :town and supplier.address.postcode like :postcode and bitwise_and(supplier.bitMask, :bitMask) > 0 order by supplier.shortName
2005-05-12 18:31:10,002 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.QueryTranslatorImpl] > SQL: select count(*) as col_0_0_ from pg_supplier supplier0_ where ((supplier0_.ShortName like ?) and (supplier0_.Town like ?) and (supplier0_.Postcode like ?) and supplier0_.bitMask & ?>0) order by supplier0_.ShortName
However, under Hibernate 3.0.3 my SQLFunction is never called with the result that the HQL -> SQL translation does not occur:
Code:
2005-05-12 18:59:46,030 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.ErrorCounter] > throwQueryException() : no errors
2005-05-12 18:59:46,040 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.QueryTranslatorImpl] > HQL: select count(*) from com.purplegem.domain.supplier.Supplier supplier where supplier.shortName like :shortName and supplier.address.town like :town and supplier.address.postcode like :postcode and bitwise_and(supplier.bitMask, :bitMask) > 0 order by supplier.shortName
2005-05-12 18:59:46,040 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.QueryTranslatorImpl] > SQL: select count(*) as col_0_0_ from pg_supplier supplier0_ where (supplier0_.ShortName like ?) and (supplier0_.Town like ?) and (supplier0_.Postcode like ?) and bitwise_and(supplier0_.bitMask, ?)>0 order by supplier0_.ShortName
2005-05-12 18:59:46,040 [Request Handler 1] [DEBUG] [2] [org.hibernate.hql.ast.ErrorCounter] > throwQueryException() : no errors
So, it looks like my custom SQLFunction is just not being applied.
Given that the only difference between it working and not working is a different version of Hibernate, could this be a bug that has been introduced to Hibernate or am I doing something wrong?
Regards,
Darren Scott