一尘不染

Hibernate 3.6:SQL方言中的registerFunction无法正常工作

hibernate

我放弃了,问社区…

在我的项目中,我使用的是Hibernate 3.6.4.Final和自定义的SQL方言:

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"));
    }

}

hasflags在HQL查询中使用该方法失败。这是查询:

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());

错误:

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)

因此,似乎该功能未正确注册。我已经使用较旧的Hibernate版本完成了几次,并且一直都以这种方式工作。

以防万一有人问:是的,我将Hibernate配置为使用方言:

<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>

编辑:在SELECT子句中,我可以使用已注册的函数而不会出现问题,但不能在WHERE子句中使用。


阅读 733

收藏
2020-06-20

共1个答案

一尘不染

我有一个类似的问题。如果将where子句修改为:

hasflags(o.status, :status) = true

我需要它在没有它的情况下工作,而对它的搜索将我带到了这里。

2020-06-20