Bil*_*mus 11 stored-procedures hibernate jpa function postgresql-9.1
我有一个问题,我得到一个:org.hibernate.MappingException:没有JDBC类型的Dialect映射:1111尝试使用JPA创建本机查询时调用postgres函数.
我在一个启动单例中创建了一个EJB计时器,每6个小时运行一次Postgres函数.该函数返回void并检查过期记录,删除它们并更新某些状态.它不需要参数,它返回void.
如果我使用PgAdmin查询工具(select function();)调用它并返回void,则postgres函数运行完美.
当我在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
Run Code Online (Sandbox Code Playgroud)
这是代码:
首先是JPA的东西:
public void runRequestCleanup() {
String queryString = "SELECT a_function_that_hibernate_chokes_on()";
Query query = em.createNativeQuery(queryString);
Object result = query.getSingleResult();
}
Run Code Online (Sandbox Code Playgroud)
这是单身人士称之为:
@Startup
@Singleton
public class RequestCleanupTimer {
@Resource
TimerService timerService;
@EJB
UserQueryBean queryBean;
@PostConstruct
@Schedule(hour = "*/6")
void runCleanupTimer() {
queryBean.runRequestCleanup();
}
}
Run Code Online (Sandbox Code Playgroud)
功能:
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;
Run Code Online (Sandbox Code Playgroud)
bez*_*max 19
这可能是一个黑客,但它对我有用,而且非常简单.只需将查询更改为:
SELECT count(*) FROM your_function();
Run Code Online (Sandbox Code Playgroud)
现在它返回一个正确的整数,而hibernate很高兴.
我已经对 JPA 搞得够多了,试图让它运行一个存储过程。
我最终使用带有准备好的语句的 JDBC。在花了几个小时试图将方钉插入圆孔后,我在 15 分钟内做到了。我调用了我的持久性单元用来获取连接的同一个 jndi 数据源,创建了一个准备好的语句并在完成后关闭它。
因此,如果您需要从(现在主要是)JPA 应用程序运行存储过程(或 Postgres 函数),这对我有用:
@Stateless
@LocalBean
public class UserQueryBean implements Serializable {
@Resource(mappedName="jdbc/DatabasePool")
private javax.sql.DataSource ds;
...
public void runRequestCleanup() {
String queryString = "SELECT cleanup_function_that_hibernateJPA_choked_on()";
Connection conn = null;
PreparedStatement statement = null;
try {
conn = ds.getConnection();
statement = conn.prepareCall(queryString);
statement.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(UserQueryBean.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
statement.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(UserQueryBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
// bit of logging code here
}
...
}
Run Code Online (Sandbox Code Playgroud)
从 JPA 中忽略在服务器上运行函数或存储过程的简单能力似乎是一个可怕的疏忽;尤其是除了 void 或受影响的行数之外不返回任何内容的程序。如果是故意的……不予置评。
编辑:添加关闭连接。
| 归档时间: |
|
| 查看次数: |
11947 次 |
| 最近记录: |