Hibernate 3.6:SQL方言中的registerFunction不起作用 [英] Hibernate 3.6: registerFunction in SQL dialect not working

查看:108
本文介绍了Hibernate 3.6:SQL方言中的registerFunction不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的项目中,我使用Hibernate 3.6.4.Final和一个自定义的sql语言:

  public class ServiceAppMySQL5InnoDBDialect extends MySQL5InnoDBDialect {
$ b $ public ServiceAppMySQL5InnoDBDialect(){
super() ;
registerFunction(bitwise_and,new SQLFunctionTemplate(StandardBasicTypes.INTEGER,(?1&?2)));
registerFunction(hasflags,新的SQLFunctionTemplate(StandardBasicTypes.BOOLEAN,?1&?2 =?2));
}

}

使用 HQL查询中的hasflags 方法失败。
这里是查询:

$ $ p $ $ $ $ c $查询q = em
.createQuery(
SELECT o FROM
+ entityClass.getName()
+o WHERE hasflags(o.status,:status)AND o.email =:email)
.setParameter(email,用户名)
.setParameter(status,status.getBitmask());

错误:

 原因:org.hibernate.hql.ast.QuerySyntaxException:意外的AST节点:(靠近第1行,第50列[SELECT o FROM tv.px.domain.Owner o WHERE hasflags(o.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)
在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>
<属性>
< property name =hibernate.dialectvalue =tv.px.persistence.hibernate.ServiceAppMySQL5InnoDBDialect/>

<! - 等等 - >
< / properties>
< / persistence-unit>编辑:在SELECT子句中,我可以使用没有问题的注册函数,但不能在WHERE子句中使用。

解决方案

我有类似的问题。它可以工作,如果你修改where子句到:

  hasflags(o.status,:status)= true 

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


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.

解决方案

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.

这篇关于Hibernate 3.6:SQL方言中的registerFunction不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆