Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

286
Visualizações
Returning Postgresql json in Entity Manager

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.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda