hibernate - H2 查询失败并显示 'No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode'

标签 hibernate h2

我有一个可与 postgres 配合使用的自定义查询,但在 H2 中失败。

select distinct(to_char(date_requested,'YYYY')) 
from Product 
where date_requested is not null

我得到的异常(exception)是:

> Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
     \-[METHOD_CALL] MethodNode: '('
        +-[METHOD_NAME] IdentNode: 'to_char' {originalText=to_char}
        \-[EXPR_LIST] SqlNode: 'exprList'
           +-[IDENT] IdentNode: 'date_requested' {originalText=date_requested}
           \-[QUOTED_STRING] LiteralNode: ''YYYY''

我的另一个查询与 to_char 的用法类似,效果很好:

select count(*), requester 
from Product 
where to_char(date_requested,'YYYY') = '2016' 
group by requester 
ORDER BY Count(*) desc

我有最新版本的 H2,1.4.192,所以我知道 to_char 的使用是可以的。

我在这里做错了什么?为什么第一个查询不适用于 H2?

最佳答案

我也遇到了同样的问题。

通过使用最新版本的 H2,这些 TO_DATE、TRUNC、TO_CHAR 函数都出现在 H2 中。

这里的问题是你使用 hibernate 作为 ORM 后端,而常规的 H2Dialect 不理解这些函数。您可以对该方言进行子类化以映射新函数。例如,这是我用于 TRUNC 函数的函数。

package my.awesome.project;

import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StandardBasicTypes;

public class CustomH2Dialect extends H2Dialect {

    public CustomH2Dialect() {
        super();
        registerFunction("trunc", new StandardSQLFunction("trunc", StandardBasicTypes.TIMESTAMP));
    }
}

并且您将 hibernate 设置“connection.driver_class”替换为:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

  <persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="connection.driver_class" value="org.h2.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:h2:./db/repository"/>
        <property name="hibernate.dialect" value="my.awesome.project.CustomH2Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        <property name="hibernate.show_sql" value="true" />

    </properties>
  </persistence-unit>

关于hibernate - H2 查询失败并显示 'No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40291484/

相关文章:

Spring + JUnit + H2 + JPA : Is it possible to drop-create the database for every test?

java - 提交后hibernate对象处于什么状态?

java - 为什么在第三次执行此代码时运行 JUnit 测试时会出现错误?

java - 春袋鼠 : JDBC driver not available for 'org.h2.Driver'

java - spring-boot 更新到 1.3.1 后 webapp 无法启动

java - 将数据插入数据库 - o​​rg.h2.jdbc.JdbcSQLException : Column count does not match; SQL statement:

java - 使用 Hibernate 实体的域实体?

hibernate - 自引用列表自定义可更新顺序

java - 即使存在约束违规异常,主键也会自动递增 Mysql(5.6.34)

hibernate - 使用 Spring Data JPA 删除 SpringBoot 2.0.5.RELEASE 应用程序中的对象