2013-07-16 57 views
0

这个问题一直让我很沮丧,我希望有人能帮助我。我正在使用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之外,还有什么特别需要做的,因为这是远程调用的?请有人帮我解决这个问题。它把我拉上了墙!

回答

0

试试这个代码

@Override 
    public List<MBMessagesAsDiscussionPrime> findByGroupId() throws SystemException { 
     Session session = null; 
     try { 
      session = openSession(); 

      //other stuff here, eventually... 
     } catch (Exception e) { 
      throw new SystemException(e); 
     } 
    } 

HTH

+0

不幸的是,这给出了相同的错误。 –

+0

你是否在开启会话或内部终止时出错? –

+0

openSession()调用引发异常。 finally块尝试关闭会话,该会话引发另一个异常(NPE,因为会话对象仍然为空),但未捕获该异常。所以这就是技术上出错的地方,但潜在的问题是openSession()失败。 –

0

使用Liferay的6.1.20和构建我用行家与Liferay的插件版本6.2.10.9。

确保您的MBMessagesAsDiscussionPrimeFinderImpl类位于service.persistence包/文件夹中。

当服务构建器运行时,它会为您的WhateverFinderImpl生成接口。在你的情况MBMessagesAsDiscussionPrimeFinderImpl。另外它增加了代码来将它实例化到你的WhateverLocalServiceImpl中。在你的情况下,它似乎是MBMessagesAsDiscussionPrimeServiceImpl。

如果您MBMessagesAsDiscussionPrimeServiceImpl类相当于WhateverLocalServiceImpl那么它应该已经在你的MBMessagesAsDiscussionPrimeFinderImpl类实例化内它,你可以改变你的代码下面,它会工作。

@JSONWebService 
public class MBMessagesAsDiscussionPrimeServiceImpl extends MBMessagesAsDiscussionPrimeServiceBaseImpl { 
    @Override 
    public List<MBMessagesAsDiscussionPrime> getMessagesAsDiscussionPrime() throws SystemException { 
     return mBMessagesAsDiscussionPrimeFinder.findByGroupId(); 
    } 
} 

对于mBMessagesAsDiscussionPrimeFinder,骆驼情况可能会有所不同。

+0

你会使用session = openSession();像其他答复者所建议的那样。 – ElectronicBlacksmith