

The Custom Liferay service function can not open session ()
source link: https://www.codesd.com/item/the-custom-liferay-service-function-can-not-open-session.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The Custom Liferay service function can not open session ()
This problem has been frustrating me for quite some time and I'm hoping someone can help me out here. I am using Service Builder to expose a custom entity to the JSON Web Service API, which I want to use in my portlet. I cannot use Dynamic Queries, and since there will be several more complicated queries later on with multiple joins, I feel custom sql is the best option. However, I'm not even able to begin the query because the call to openSession() throws a NPE. Here's my code (I apologize for the length, but I really have no idea what I'm doing wrong here and I'm just trying to include everything that's relevant):
ServiceImpl class:
@JSONWebService
public class MBMessagesAsDiscussionPrimeServiceImpl extends MBMessagesAsDiscussionPrimeServiceBaseImpl {
@Override
public List<MBMessagesAsDiscussionPrime> getMessagesAsDiscussionPrime() throws SystemException {
MBMessagesAsDiscussionPrimeFinder finder = new MBMessagesAsDiscussionPrimeFinderImpl();
return finder.findByGroupId();
}
}
My FinderImpl class:
public class MBMessagesAsDiscussionPrimeFinderImpl extends BasePersistenceImpl<MBMessagesAsDiscussionPrime> implements MBMessagesAsDiscussionPrimeFinder {
@Override
public List<MBMessagesAsDiscussionPrime> findByGroupId() throws SystemException {
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
try {
session = sessionFactory.openSession(); //exception here
//other stuff here, eventually...
} catch (Exception e) {
throw new SystemException(e);
} finally {
closeSession(session); //throws NPE here
}
}
}
custom query:
<?xml version="1.0" encoding="UTF-8"?>
<custom-sql>
<sql
id="com.test.portlet.service.persistence.MBMessagesAsDiscussionPrimeFinder.findByGroupId">
<![CDATA[
SELECT * FROM MBMessage, MBThread
WHERE
(MBMessage.threadId = MBThread.threadId) AND
(MBThread.groupID = ?)
ORDER BY
MBThread.rootMessageId DESC, MBMessage.messageId ASC
]]>
</sql>
</custom-sql>
and service.xml:
<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC
"-//Liferay//DTD Service Builder 6.1.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.test.portlet">
<namespace>MBMessagesAsDiscussionPrime</namespace>
<entity name="MBMessagesAsDiscussionPrime" uuid="true" local-service="true" remote-service="true">
<column name="messageId" type="long" primary="true" />
<column name="threadId" type="long"/>
<column name="userId" type="long"/>
<column name="userName" type="String"/>
<column name="body" type="String"/>
<reference package-path="com.liferay.portlet.messageboards" entity="MBMessage" />
<reference package-path="com.liferay.portlet.messageboards" entity="MBThread" />
</entity>
</service-builder>
The service function is visible from localhost:8080/custom-query-portlet/api/jsonws, which is where I'm calling it from. Is there something in particular I need to do since this is being called remotely, besides setting @JSONWebService on the ServiceImpl class? Please, someone help me out on this. It's driving me up a wall!
Try this code
@Override
public List<MBMessagesAsDiscussionPrime> findByGroupId() throws SystemException {
Session session = null;
try {
session = openSession();
//other stuff here, eventually...
} catch (Exception e) {
throw new SystemException(e);
}
}
Recommend
-
15
README.markdown Liferay Portal Liferay Portal is an open source enterprise web platform for building...
-
23
前言: 最近liferay portal被爆了一个json的反序列化漏洞,本着学习的态度准备研究一番,于是搭建了低版本环境,顺手搜了下readObject函数,意外发现TunnelServlet存在java反序列化漏洞,想...
-
15
F# Compiler Service - F# Compiler Community Session193 views•Jan 21, 2021
-
27
3 min readCVE-2020-7961 Liferay Portal 反序列化RCE分析2020-03-29Code White 公开了 Liferay Portal JSON反序列化RCE漏洞,攻击者可以发送payload到服务器造成远程代码执行。2020/3/24日,陈师傅
-
8
Rajneesh Mohan Kumar October 7, 2021 2 minute read ...
-
3
In this blog I'd like to tell you how to use Eclipse with Liferay to develop portlets with the ability to change a class or a JSP in Eclipse and have that immediatelly reflected on the server (hot deployment). EnvironmentLiferay...
-
13
How do I open a window in another X11 session? advertisements linux only: i want to open two windows for my app...
-
6
Monica Domingo June 21, 2022 Less than a 1 minute r...
-
9
POC Liferay RCE(CVE-2020-7961) · GitHub Instantly share code, notes, and snippets. ...
-
5
Deep Dive workshop for Oil, Gas & Energy and Open-Door Session on S/4HANA Transformation for Oil, Gas & Utilities at International Conference for Oil, Gas, Energy & Utilities
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK