I'm attempting to use ENTITY MANAGER to return a POSTGRESQL 9.6 Stored Procedure/Function JSON result. THIS RETURNS 1 RECORD ONLY.
**POSTGRESQL TABLES**
TABLE: **customer**
[enter image description here][1]
TABLE: **names**
[enter image description here][2]
TABLE: **address**
[enter image description here][3]
**POSTGRESQL STORED PROCEDURE**
CREATE OR REPLACE FUNCTION jsp_member_main(
IN cid numeric,
OUT t json)
RETURNS json
LANGUAGE 'sql'
COST 100.0
VOLATILE
AS $function$
select json_agg(t)
from (SELECT
c.id customer_id,
n.first_name,
n.last_name,
a.street1,
a.city,
a.state,
a.postal_code
FROM customer c
LEFT OUTER JOIN names n ON (c.name_id = n.id)
LEFT OUTER JOIN address a ON (c.id = a.customer_id)
WHERE c.id=cid
) t;
$function$
**POSTGRESQL STORED PROCEDURE QUERY**
select jsp_member_main(99964);
**RETURNS VIA SQL**
[{"customer_id":99964,"first_name":"JOHN","last_name":"SMITH","street1":"123 W. MAIN ST.","city":"SPINDALE","state":"MD","postal_code":"21791"}]
So far, so good. Now I try to use ENTITY MANAGER to return results:
@GET
@Path("SP Member Main")
@Produces({"application/xml", "application/json"})
public JSONObject memberMain() throws JSONException {
EntityManager em = getEntityManager();
StoredProcedureQuery storedProcedure = em.createStoredProcedureQuery("jsp_member_main");
storedProcedure.registerStoredProcedureParameter("cid", Integer.class, ParameterMode.IN);
storedProcedure.registerStoredProcedureParameter("t", JSONObject.class, ParameterMode.OUT);
storedProcedure.setParameter("cid", 99964);
storedProcedure.execute();
JSONObject retval = (JSONObject) storedProcedure.getOutputParameterValue("t");
return retval;
}
No matter what return (OUT) type class I try, I get the following error:
***javax.ejb.EJBException
root cause
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: A CallableStatement function was executed and the out parameter 1 was of type java.sql.Types=1111 however type java.sql.Types=12 was registered.
Error Code: 0
Call: {?= CALL ross.jsp_member_main(?)}
bind => [2 parameters bound]
Query: ResultSetMappingQuery()***
I've tried as String class, returning toString, JSONArray cobverting JSONObject to JSONArray, java OBJECT, etc. Nothing works.
Is there a way through ENTITY MANAGER to return json from a Postgresql stored procedure/function? Many thanks for your consideration.