use of entityManager.createNativeQuery(query,foo.class)

后端 未结 5 671
-上瘾入骨i
-上瘾入骨i 2020-12-01 07:49

I would like to return a List of Integers from a

javax.persistence.EntityManager.createNativeQuery call

Why is the following incorrect?

5条回答
  •  余生分开走
    2020-12-01 08:15

    Here is a DB2 Stored Procidure that receive a parameter

    SQL

    CREATE PROCEDURE getStateByName (IN StateName VARCHAR(128))
    DYNAMIC RESULT SETS 1
    P1: BEGIN
        -- Declare cursor
        DECLARE State_Cursor CURSOR WITH RETURN for
        -- #######################################################################
        -- # Replace the SQL statement with your statement.
        -- # Note: Be sure to end statements with the terminator character (usually ';')
        -- #
        -- # The example SQL statement SELECT NAME FROM SYSIBM.SYSTABLES
        -- # returns all names from SYSIBM.SYSTABLES.
        -- ######################################################################
        SELECT * FROM COUNTRY.STATE
        WHERE PROVINCE_NAME LIKE UPPER(stateName);
        -- Cursor left open for client application
        OPEN Province_Cursor;
    END P1
    

    Java

    //Country is a db2 scheme
    
    //Now here is a java Entity bean Method
    
    public List getStateByName(String stateName) throws Exception {
    
        EntityManager em = this.em;
        List states= null;
        try {
            Query query = em.createNativeQuery("call NGB.getStateByName(?1)", Province.class);
            query.setParameter(1, provinceName);
            states= (List) query.getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    
        return states;
    }
    

提交回复
热议问题