如何在HQL查询中使用本机sql函数?

时间:2016-03-11 01:26:09

标签: oracle hibernate grails hql gorm

我正在尝试使用HQL执行如下所示的查询,该查询使用本机SQL函数(dbms_lob.getlength):

def results = Attachment.executeQuery(
    'select id, originalFilename, dbms_lob.getlength(a.fileBytes), dateCreated, createUserName '+
    'from Attachment a where a.id not in '+
         '(select attachmentId from SpecVersion sv where sv.attachmentId is not null) '+
    'and a.dateCreated > sysdate - 30')
  • fileBytes列是BLOB - 我正在尝试获取BLOB内容的大小。

但是,这会导致以下错误。

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode
 +-[METHOD_CALL] MethodNode: '('
 |  +-[METHOD_NAME] IdentNode: 'dbms_lob.getlength' {originalText=dbms_lob.getlength}
 |  \-[EXPR_LIST] SqlNode: 'exprList'
 |     \-[DOT] DotNode: 'attachment0_.file_bytes' {propertyName=fileBytes,dereferenceType=ALL,propertyPath=fileBytes,path=a.fileBytes,tableAlias=attachment0_,className=com.et.layoutmgr.grails.mapping.Attachment,classAlias=a}
 |        +-[ALIAS_REF] IdentNode: 'attachment0_.ATTACHMENT_ID' {alias=a, className=com.et.layoutmgr.grails.mapping.Attachment, tableAlias=attachment0_}
 |        \-[IDENT] IdentNode: 'fileBytes' {originalText=fileBytes}

        at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:156)
        at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:857)
        at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:645)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:685)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
        at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:256)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
        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.codehaus.groovy.grails.orm.hibernate.metaclass.ExecuteQueryPersistentMethod$2.doInHibernate(ExecuteQueryPersistentMethod.java:81)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
        at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
        at org.codehaus.groovy.grails.orm.hibernate.metaclass.ExecuteQueryPersistentMethod.doInvokeInternal(ExecuteQueryPersistentMethod.java:79)
        at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractStaticPersistentMethod.invoke(AbstractStaticPersistentMethod.java:72)
        at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractStaticPersistentMethod.invoke(AbstractStaticPersistentMethod.java:65)

使用HQL是否可以这样,或者我是否需要切换到使用native SQL

3 个答案:

答案 0 :(得分:3)

我通过在Overwritten dialect中添加这样的自定义函数来实现这一点。

class MySqlDialect extends MySQL5InnoDBDialect {

    public MySqlDialect() {
        super();
        registerFunction("date_add_interval", new SQLFunctionTemplate(StandardBasicTypes.DATE, "date_add(?1, INTERVAL ?2 ?3)"));
        registerFunction("date_sub_interval", new SQLFunctionTemplate(StandardBasicTypes.DATE, "date_sub(?1, INTERVAL ?2 ?3)"));
        registerFunction("to_date", new SQLFunctionTemplate(StandardBasicTypes.DATE, "str_to_date(?1, ?2)"));
        registerFunction("minutes_diff", new SQLFunctionTemplate(StandardBasicTypes.LONG, "timestampdiff(MINUTE, ?1, ?2)"));
    }

    public String openBlobSelectQuote() {
        return "`";
    }

    public String closeBlobSelectQuote() {
        return "`";
    }
}

更新: 功能注册:

    registerFunction("getlength", new SQLFunctionTemplate(StandardBasicTypes.LONG, "dbms_lob.getlength(?1)"));

您的HQL查询:

def results = Attachment.executeQuery(
    'select id, originalFilename, getlength(a.fileBytes), dateCreated, createUserName '+
    'from Attachment a where a.id not in '+
         '(select attachmentId from SpecVersion sv where sv.attachmentId is not null) '+
    'and a.dateCreated > sysdate - 30')

答案 1 :(得分:3)

Attachment域中添加新字段Long fileBytesLength并在内部映射闭包中添加用于计算fileBytes字段长度的公式。

class Attachment {

    String createUserName
    String originalFilename

    byte[] fileBytes
    Long fileBytesLength

    Date dateCreated
    //Other Properties

    static mapping = {
        //Other mappings
        fileLength formula: "dbms_lob.getlength(fileBytes)"
    }
}

然后将查询修改为:

def results = Attachment.executeQuery(
    'select id, originalFilename, fileBytesLength, dateCreated, createUserName '+
    'from Attachment a where a.id not in '+
         '(select attachmentId from SpecVersion sv where sv.attachmentId is not null) '+
    'and a.dateCreated > sysdate - 30')

答案 2 :(得分:0)

您始终可以使用基本的java jdbc连接来执行查询。为此创建java代码并将其存储在src / java /.