user503285 user503285 - 9 months ago 112
Java Question

calling a mysql function returning varchar in hibernate

I am trying to use mysql

string function in hibernate but unfortunately I cant able to use it and even tried by creating these register functions

registerFunction("findInSet",new StandardSQLFunction("find_in_set", Hibernate.INTEGER));

using mysqldialect but no one is working..can any one please tell me how to use the mysql string functions in hibernate. or any changes to the above register functions.

Thanks in Advance,

Best Regards,

Answer Source

created a class by extending mysqldialect like shown below

public class MySqlDialectExtended extends MySQLDialect {

public MySqlDialectExtended() {
    registerFunction("date_add_interval", new SQLFunctionTemplate(Hibernate.DATE, "date_add(?1, INTERVAL ?2 ?3)"));
    registerFunction("date_sub_interval", new SQLFunctionTemplate(Hibernate.DATE, "DATE_SUB(?1, INTERVAL ?2 ?3)"));
    registerFunction("weekofyear", new StandardSQLFunction("weekofyear", Hibernate.INTEGER));
    registerFunction("group_concat", new StandardSQLFunction("group_concat", Hibernate.STRING));


and used it like this in query

new Query(" >= date_sub_interval(CURRENT_DATE(),1, DAY)");