Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 3.6: registerFunction in SQL dialect not working

I´m giving up and ask the community ...

In my project, I´m using Hibernate 3.6.4.Final and a custom sql dialect:

public class ServiceAppMySQL5InnoDBDialect extends MySQL5InnoDBDialect {

    public ServiceAppMySQL5InnoDBDialect() {
        super();
        registerFunction("bitwise_and", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "(?1 & ?2)"));
        registerFunction("hasflags", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "?1 & ?2 = ?2"));
    }

}

Using the hasflags method in a HQL query fails. Here is the query:

Query q = em
        .createQuery(
            "SELECT o FROM "
            + entityClass.getName()
            + " o WHERE hasflags(o.status, :status) AND o.email = :email")
        .setParameter("email", username)
        .setParameter("status", status.getBitmask());

The error:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 50 [SELECT o FROM tv.px.domain.Owner o WHERE hasflags(o.status, :status) AND o.email = :email]
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)

So, it seems as if the function is not properly registered. I´ve done this several times with older Hibernate versions and it worked this way all the time.

Just in case someone asks this: yes, I configured Hibernate to use the dialect:

<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="tv.px.persistence.hibernate.ServiceAppMySQL5InnoDBDialect" />

        <!-- and so on -->
    </properties>
</persistence-unit>

Edit: In the SELECT clause I can use registered functions without problems, but not in the WHERE clause.

like image 648
Konsumierer Avatar asked May 24 '11 15:05

Konsumierer


1 Answers

I have a similar problem. It works if you modify the where clause to:

hasflags(o.status, :status) = true

I need it to work without that, and my search for it brought me here.

like image 99
Vithun Avatar answered Oct 18 '22 12:10

Vithun