6

The Custom Liferay service function can not open session ()

 3 years ago
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 ()

advertisements

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);
        }
    }




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK