简体   繁体   中英

How to return the generated value of a stored procedure using hibernate

I am new to hibernate i got a scenario where i need to call a stored procedure using hibernate.I am using native query to call my stored procedure.The code below is where i am setting all the parameters to the stored proc.And i declared my query in my entity.

ENTITY class:


      @Entity
        @NamedNativeQueries({
            @NamedNativeQuery(
            name = "callStoreProcedure",
            query = "CALL SP_INS_EPC_CERTIFICATE_EVENTS(:BUS_PARTNER_ID ,:PROVIDER_NAME,:PROVIDER_ADDR_LINE1,:PROVIDER_ADDR_LINE2 ,:PROVIDER_CITY,:PROVIDER_STATE,EPC_CERTIFICATE)",
        resultClass = DOEPCTest.class)

        })

Stored procedure:

CREATE PROCEDURE EPRMTADM.SP_INS_EPC_CERTIFICATE_EVENTS
(
          -- IN EPC_CERTIFICATE_ID INTEGER,
          IN P_BUS_PARTNER_ID CHAR(6),
          IN P_PROVIDER_NAME VARCHAR(80),
          IN P_PROVIDER_ADDR_LINE1 VARCHAR(80),
          IN P_PROVIDER_ADDR_LINE2 VARCHAR(80),
          IN P_PROVIDER_CITY VARCHAR(80),
          IN P_PROVIDER_STATE CHAR(2),
          OUT P_EPC_CERTIFICATE_ID INTEGER
)

(....INSERT Statments which sets all the values in to the PROC)

SET P_EPC_CERTIFICATE_ID = IDENTITY_VAL_LOCAL();

OUT P_EPC_CERTIFICATE_ID INTEGER returns the certificate_id

Query query = sessionFactory.getCurrentSession().getNamedQuery("callStoreProcedure").
                setParameter("BUS_PARTNER_ID", "0123");
                setParameter("PROVIDER_NAME", "qwe");
                query.setParameter("PROVIDER_ADDR_LINE1", "asdasd");
                query.setParameter("PROVIDER_ADDR_LINE2", "aasd");  
                query.setParameter("PROVIDER_CITY", "asd");
                query.setParameter("PROVIDER_STATE", "pa");
                        List result = query.list();

As my CERTIFICATE_ID is generated value i cannot set it,It is the return value.How can i handle it in hibernate.Is there any standard api which returns the OUT P_EPC_CERTIFICATE_ID INTEGER out

And this is identity key generated by DB2 when i call this Store Procedure.Is there any way to get this certificate id out.I am able to set the parameters in but.I coudnt get this value out.I think i should pass some thing like.

   procedure.registerOutParameter(EPC_CERTIFICATE_ID , java.sql.Types.INTEGER);

But i have no idea how to do it using Query in hibernate. please suggest a best way to get the identity get out. thanks.

Wont you get the certificate like this ?

for(int i=0; i<result.size(); i++){
        DOEPCTest doepcTest = (DOEPCTest)result.get(i);
        System.out.println(doepcTest.getEPC_CERTIFICATE_ID());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM