1

I'm trying to call a stored procedure on a PostgreSQL db. The procedure handles inserting, updating, and deleting table entries, so it accepts params. Some of the params are sometimes null, depending on the kind of operation you want to perform. I was able to call the proc using Hibernate's SessionFactory, but now I want to use the JPA EntityManager. The procedure using SessionFactory is as 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 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();
}

As you can see, I specify the type of the field twice: one for Java, one for Postgres. Now if I do the same using the EntityManager, there is no setParameter method that accepts LongType.INSTANCE as an argument. Is there any workaround to this?

EDIT: Also looking for a way to do this using jdbcTemplate. Tried doing this way: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html#jdbc-simple-jdbc-call-1, but got a NullPointerException on the .execute(in) line.

1 Answer 1

1

May be you should use EntityManager.createStoredProcedureQuery. In this way you can set type for parameters, for 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 for Oracle may be it's help you.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. I'll try it and get back to you.
Now the db is saying that I didn't pass a parameter to the function. I did pass it and it was null.
Are you speak about 5 parameter? In my answer it is null.
Yes, I pass it, and the database doesn't recognize it as a passed parameter. Do you know of a way to cast the parameter before it's passed? Like I did with session factory: CAST(:cat_id AS BIGINT)
Why are you cant cast param into java code? For example change LongType.INSTANCE to Integer.class or Number.class. If you know that you put integer param.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.