这个问题一直让我很沮丧,我希望有人能帮助我。我正在使用Service Builder将自定义实体公开到JSON Web服务API,我想在我的Portlet中使用它。我无法使用动态查询,并且由于稍后会有多个连接的复杂查询,我觉得自定义sql是最佳选择。但是,我甚至无法开始查询,因为调用openSession()会引发NPE。这里是我的代码(我的长道歉,但我真的不知道我在做什么错在这里,我只是想包括一切的有关):定制Liferay服务功能失败openSession()
ServiceImpl类:
@JSONWebService
public class MBMessagesAsDiscussionPrimeServiceImpl extends MBMessagesAsDiscussionPrimeServiceBaseImpl {
@Override
public List<MBMessagesAsDiscussionPrime> getMessagesAsDiscussionPrime() throws SystemException {
MBMessagesAsDiscussionPrimeFinder finder = new MBMessagesAsDiscussionPrimeFinderImpl();
return finder.findByGroupId();
}
}
我FinderImpl类:
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
}
}
}
自定义查询:
<?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>
和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>
的服务功能是可见的从本地主机:8080 /自定义查询的portlet/API/jsonws,这就是我从调用它。除了在ServiceImpl类上设置@JSONWebService之外,还有什么特别需要做的,因为这是远程调用的?请有人帮我解决这个问题。它把我拉上了墙!
不幸的是,这给出了相同的错误。 –
你是否在开启会话或内部终止时出错? –
openSession()调用引发异常。 finally块尝试关闭会话,该会话引发另一个异常(NPE,因为会话对象仍然为空),但未捕获该异常。所以这就是技术上出错的地方,但潜在的问题是openSession()失败。 –