简体   繁体   中英

How do I pass more than one parameter in Hibernate createSQLQuery()?

I am trying to call a Stored Procedure using Hibernate. I can't seem to find how I pass more than one parameter?

I know with one parameter you can do this:

Session session = HibernateUtil.getSessionFactory().openSession();
...
Query query = session.createSQLQuery("CALL StoredProcedureTest(:parameter)")
.addEntity(DBModel.class)
.setParameter("parameter", parameter);

ArrayList<DBModel> results = query.list();

I notice there is a .setParameters(Object object[], Type[] types) option but can't find many examples of this at all and don't see how this would work.

What do I do if I need more than one parameter? Can I do this in the same way?

Call setParameter another time:

Query query = session.createSQLQuery("CALL StoredProcedureTest(:parameter1, :parameter2)")
   .addEntity(DBModel.class)
   .setParameter("parameter1", parameter1)
   .setParameter("parameter2", parameter2);

You can always continue the line:

Query query = session.createSQLQuery("CALL StoredProcedureTest(:parameter, :parameter2)")
.addEntity(DBModel.class)
.setParameter("parameter", parameter).setParameter("parameter2", parameter2);

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