java - Calling a PostgreSQL Proc using EntityManager -
i'm trying call stored procedure on postgresql db. procedure handles inserting, updating, , deleting table entries, accepts params. of params null, depending on kind of operation want perform. able call proc using hibernate's sessionfactory, want use jpa entitymanager. procedure using sessionfactory follows:
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 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(); }
as can see, specify type of field twice: 1 java, 1 postgres. if same using entitymanager, there no setparameter method accepts longtype.instance argument. there workaround this?
edit: looking way using jdbctemplate. tried doing way: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html#jdbc-simple-jdbc-call-1, got nullpointerexception on .execute(in) line.
may should use entitymanager.createstoredprocedurequery
. in way can set type parameters, example:
private final static string stored_procedure = "public.somefunction"; storedprocedurequery query = em.createstoredprocedurequery(stored_procedure); query. registerstoredprocedureparameter(1, string.class, parametermode.in).setparameter(1, type_proc). registerstoredprocedureparameter(2, string.class, parametermode.in).setparameter(2, node). registerstoredprocedureparameter(3, string.class, parametermode.in).setparameter(3, fullcode). registerstoredprocedureparameter(4, date.class, parametermode.in).setparameter(4, bdate). registerstoredprocedureparameter(5, longtype.instance, parametermode.in).setparameter(5, null). execute();
i used entitymanager.createstoredprocedurequery
oracle may it's you.
Comments
Post a Comment