JPA Hibernate调用Postgres函数void返回MappingException:

前端之家收集整理的这篇文章主要介绍了JPA Hibernate调用Postgres函数void返回MappingException:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个问题,我得到一个:org.hibernate.MappingException:没有Dialect映射JDBC类型:1111当尝试使用JPA创建本机查询调用postgres函数.

我在启动单例中创建了一个EJB定时器,每6小时运行一次Postgres函数.该函数返回void并检查过期记录,删除它们并更新某些状态.它不需要参数,它返回void.

>如果我使用PgAdmin查询工具(select function();)调用它,postgres函数运行完美,并返回void.
>当我在Glassfish 3.1.1上部署应用程序时,我会遇到异常和部署失败.

这是(缩短)堆栈跟踪:

WARNING: A system exception occurred during an invocation on EJB UserQueryBean method public void com.mysoftwareco.entity.utility.UserQueryBean.runRequestCleanup()
javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
...STACK TRACE BLAH BLAH BLAH ...
Caused by: javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111

这是代码

首先是JPA的东西:

public void runRequestCleanup() {
    String queryString = "SELECT a_function_that_hibernate_chokes_on()";
    Query query = em.createNativeQuery(queryString);
    Object result = query.getSingleResult();
}

这是单身号召:

@Startup
@Singleton
public class RequestCleanupTimer {
    @Resource
    TimerService timerService;
    @EJB
    UserQueryBean queryBean;

    @PostConstruct
    @Schedule(hour = "*/6")
    void runCleanupTimer() {
        queryBean.runRequestCleanup();
    }
}

功能

CREATE OR REPLACE FUNCTION a_function_that_hibernate_chokes_on()
  RETURNS void AS
$BODY$
    DECLARE 
        var_field_id myTable.field_id%TYPE;
    BEGIN
        FOR var_field_id IN
                select field_id from myTable 
                where status = 'some status'
                and disposition = 'some disposition'
                and valid_through < now()
        LOOP
            BEGIN
                -- Do Stuff
            END;
        END LOOP;
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

解决方法

这可能是一个黑客,但它对我来说很简单.只需将查询更改为:
SELECT count(*) FROM your_function();

现在它返回一个正确的整数,休眠是快乐的.

猜你在找的Java相关文章