问题
I am referring Using dynamic query in Liferay and using MySQL 5.5 but instead of custom queries involving multiple entities,we need to call a stored procedure. We have created a sample procedure
delimiter //
Create Procedure proc_check (OUT count INT)
begin
select count(*) into count from lg_office ;
end//
In default.xml,containing custom queries,we have used
<sql id="de.uhh.l2g.plugins.service.persistence.ProducerFinder.findOfficeCount">
<![CDATA[
Call proc_check(@output)
]]>
</sql>
In the respective Finder method,we used the below snippet to call the stored proc,passing -1 for both begin and end.
String sql = CustomSQLUtil.get(FIND_OFFICE_COUNT);
SQLQuery q = session.createSQLQuery(sql);
QueryPos qPos = QueryPos.getInstance(q);
//qPos.add(lectureseriesId);
List <Integer> sl = (List<Integer>) QueryUtil.list(q, getDialect(), begin, end);
return sl;
In QueryUtil,we could not find other applicable methods to execute the call. Post this we get the below error
ERROR [RuntimePageImpl-5][JDBCExceptionReporter:82] ResultSet is from UPDATE. No Data.
Is this approach correct with something missing or if not,please suggest approach to achieve the same.
回答1:
Look at this, try it.
session = openSession();
String sql = CustomSQLUtil.get(DELETE_BY_PROJETID);
SQLQuery query = session.createSQLQuery(sql);
query.setCacheable(false);
QueryPos qPos = QueryPos.getInstance(query);
qPos.add(projectId);
query.executeUpdate();
https://web.liferay.com/it/community/forums/-/message_boards/message/37490823
回答2:
there isn't any utility built-in in liferay to call stored procedure but you can just get the connection with DataAccess.getConnection();
and use the jdbc api like this way
Connection connection =DataAccess.getConnection();
CallableStatement cs = connection.prepareCall("{Call proc_check(@output)}");
ResultSet rs = cs.executeQuery();
来源:https://stackoverflow.com/questions/40926252/how-to-call-stored-procedure-in-liferay