0

Is there a way to call a stored procedure created on SQL server via java code using hibernate/jdbc with passing list of values/objects as parameters? If yes then how can i achieve it? I know how to call a stored procedure, but i have used stored procedures so far with passing only few parameters and not a complete list of values as i intend to do batch update/insert. If i do something like the following, would that make sense?

getTemplate().getSessionFactory().getCurrentSession().createSQLQuery("CALL proc_insert(:list)").setParameterList("list", objList).executeUpdate();

1 Answer 1

1

this may help Use callableStatement

String getDBUSERByUserIdSql = "{call getDBUSERByUserId(?,?,?,?)}";
callableStatement = dbConnection.prepareCall(getDBUSERByUserIdSql);
callableStatement.setInt(1, 10);
callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(4, java.sql.Types.DATE);

// execute getDBUSERByUserId store procedure
callableStatement.executeUpdate();

String userName = callableStatement.getString(2);
String createdBy = callableStatement.getString(3);
Date createdDate = callableStatement.getDate(4);

For more Info click

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

2 Comments

In the above answer, to access the underlying dbConnection object from Hibernate session, you could use getTemplate().getSessionFactory().getCurrentSession().connection()
What i want to do here is update at the max 100 records at a time. for each update i would be inserting a new row in history table.and i am absolutely clueless as to how i should approach this scenario.i dont know how to pass 100 values to a procedure and how i am going to update and insert records from the procedure. can anyone point me in the right direction? at the moment i am updating and inserting one records at a time by looping using hibernate (using dao.update(obj)).

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.