How to call stored procedure in liferay?

佐手、 提交于 2019-12-24 05:46:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!