How to get the OUTPUT value from a stored procedure in sleep mode (SQL SERVER)

I have a stored procedure in MS SQL Server

 CREATE PROC dbo.insertData @param1 nvarchar(11), @param2 nvarchar(30), @ID bigint OUTPUT AS BEGIN INSERT INTO dbo.Adaptor (Col1, Col2) VALUES (@param1, @param2) SET @ID = SCOPE_IDENTITY(); END 

And I want to get this generated identifier as an output parameter in java.

I created a mapping file

 <sql-query name="insertData" callable="true"> <return alias="adaptor" class="MyClass"> <return-property name="id" column="ID" /> </return> { ? = call InsertData(:param1, :param2) } </sql-query> 

and trying to execute it from java

  Query query = entityManager.createNamedQuery("insertData"); query.setParameter("param1", object); query.setParameter("param2", getName()); // TODO try to use getSingleResult in tests List result = query.getResultList(); 

But I have java.sql.SQLException exception: parameter # 3 is not set

Should I delete? = I got

java.sql.SQLException: procedure or function 'insertData' expects parameter '@ID' that was not specified.

Does anyone know what the code / mapping file should look like? thanks in advance

+4
source share
1 answer
 Query query = session.createSQLQuery( "CALL GetStocks(:stockCode)") .addEntity(Stock.class) .setParameter("stockCode", "7277"); List result = query.list(); for(int i=0; i<result.size(); i++){ Stock stock = (Stock)result.get(i); System.out.println(stock.getStockCode()); } 

from here

+3
source

Source: https://habr.com/ru/post/1486262/


All Articles