【发布时间】:2016-04-25 09:01:06
【问题描述】:
我正在尝试在 PostgreSQL 数据库上调用存储过程。该过程处理插入、更新和删除表条目,因此它接受参数。某些参数有时为空,具体取决于您要执行的操作类型。我能够使用 Hibernate 的 SessionFactory 调用 proc,但现在我想使用 JPA EntityManager。使用SessionFactory的过程如下:
public Result callFunction(String type_proc, String node, String fullcode, Date bdate){
Session session = this.sessionFactory.getCurrentSession();
org.hibernate.Query query = session.createSQLQuery(
"SELECT*FROM public.someFunction(:type_proc, :node, :fullcode, :bdate, CAST(:cat_id AS BIGINT)")
.addEntity(Result.class)
.setParameter("type_proc", type_proc)
.setParameter("node", node)
.setParameter("fullcode", fullcode)
.setParameter("bdate", bdate)
.setParameter("cat_id", null, LongType.INSTANCE)
return (Result) query.uniqueResult();
}
如您所见,我指定了两次字段的类型:一次用于 Java,一次用于 Postgres。现在,如果我使用 EntityManager 执行相同操作,则没有 setParameter 方法接受 LongType.INSTANCE 作为参数。有什么解决办法吗?
编辑:也在寻找一种使用 jdbcTemplate 的方法。尝试这样做:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html#jdbc-simple-jdbc-call-1,但在 .execute(in) 行上出现 NullPointerException。
【问题讨论】:
标签: java hibernate postgresql jpa stored-procedures