如何在hibernate中执行不带参数的匿名plsql块?
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
//Example (as I would like):
Query q = session.createQuery("begin select user_name into :p_name from fnd_user where user_id = :p_id; end;");
q.registerParameter("p_name", String.class, ParameterMode.OUT);
q.registerParameter("p_id", String.class, ParameterMode.IN).bindValue(100);
q.execute();
但是Query中没有注册参数。
现有的带参数注册的方法不适合:
createNamedStoredProcedureQuery
createStoredProcedureQuery
createStoredProcedureCall
添加代码来执行该过程(
begin %ORIGINAL_SQL%(%PARAMS%); end;
)。
P。 S.:香草选项工作正常:
String name = session.doReturningWork((Connection c) -> {
String sql = "declare v_str VARCHAR2(100); begin select user_name into :p_name
from fnd_user where user_id = :p_id; end;";
CallableStatement cs = c.prepareCall(sql);
cs.registerOutParameter("p_name", Types.VARCHAR);
cs.setInt("p_id", 1);
cs.execute();
return cs.getString("p_name");
});
Hibernate 无法完成此任务。您唯一的选择是声明一个存储过程并调用它。