0
votes

I am trying to create a new entity in liferay 6.1 hook customizing the calendar portlet. I used service builders to create a new entity referencing the Liferay 6.1 documentation (https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/service-builder-liferay-portal-6-1-dev-guide-en). Building the service is successful. This is the generated portlet-spring.xml file.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" default-destroy-method="destroy" default-init-method="afterPropertiesSet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="test.service.CalTimeSlotsLocalService" class="test.service.impl.CalTimeSlotsLocalServiceImpl" />
    <bean id="test.service.CalTimeSlotsService" class="test.service.impl.CalTimeSlotsServiceImpl" />
    <bean id="test.service.persistence.CalTimeSlotsPersistence" class="test.service.persistence.CalTimeSlotsPersistenceImpl" parent="basePersistence" />
</beans>

I added a custom function in xxLocalServiceImpl class , found below :

public List<String> getTimeValues() throws SystemException {
List<String> timeValues = new ArrayList<String>();
CalTimeSlotsPersistence calTimeSlotPersist = new CalTimeSlotsPersistenceImpl();
for (CalTimeSlots calTimeSlot : calTimeSlotPersist.findAll()) {
timeValues.add(calTimeSlot.getTimeValue());
}
return timeValues;
}

The entity is created in the database correctly. However a null pointer exception is thrown at the findAll function above.

09:41:03,749 ERROR [http-bio-8080-exec-4][BasePersistenceImpl:186] Caught unexpected exception java.lang.NullPointerException
09:41:03,782 ERROR [http-bio-8080-exec-4][IncludeTag:253] Current URL /web/guest/what-we-do?p_p_id=86&p_p_lifecycle=0&p_p_state=pop_up&p_p_mode=view&controlPanelCategory=portlet_86&_86_redirect=%2F&_86_struts_action=%2Fportlet_configuration%2Fedit_configuration&_86_returnToFullPageURL=%2F&_86_portletResource=8&_86_previewWidth=&tabs2=display-settings generates exception: null
09:41:03,787 ERROR [http-bio-8080-exec-4][IncludeTag:154] java.lang.NullPointerException
at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.closeSession(BasePersistenceImpl.java:74)
at test.service.persistence.CalTimeSlotsPersistenceImpl.findAll(CalTimeSlotsPersistenceImpl.java:517)
at test.service.persistence.CalTimeSlotsPersistenceImpl.findAll(CalTimeSlotsPersistenceImpl.java:416)
at test.service.impl.CalTimeSlotsLocalServiceImpl.getTimeValues(CalTimeSlotsLocalServiceImpl.java:55)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:122)
at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:71)
at com.liferay.portal.dao.jdbc.aop.DynamicDataSourceTransactionInterceptor.invoke(DynamicDataSourceTransactionInterceptor.java:44)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)
at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:57)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)
at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:57)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)
at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:57)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)
at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:57)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)
at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:57)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)
at com.liferay.portal.security.pacl.PACLAdvice.invoke(PACLAdvice.java:51)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:118)

When I tried debugging the problem I found that the session factory isn't instantiated and is always null. Is there something I am missing ? How can I solve this?

Thank You.

2
You shouldn't instantiate the CalTimeSlotsPersistenceImpl but let Spring manage it. Currently you are creating an instance spring doesn't know about and as such will not inject dependencies into.M. Deinum
Don't the persistence classes generated by the service builder reference the base persistence class? and as far as I can see in the base-spring.xml the BasePersistence class has a session factory. <bean id="basePersistence" abstract="true"> <property name="dataSource" ref="liferayDataSource" /> <property name="sessionFactory" ref="liferaySessionFactory" /> </bean> So any custom persistence class will reference the base persistence and through my debugging I found that the base persistence class creates a session factory.girlintech
That doesn't mean a thing if you don't use a spring managed instance. You are creating instances yourself... No matter how much configuration you are going to throw at it (well actually using AspectJ and compile or load time weaving would make it work) if you create your own instances spring will do nothing to those.M. Deinum
Thanks for your fast response :) I used the xxUtil class to get all rows in my table and it worked correctly without exceptions.girlintech

2 Answers

0
votes

You can use getCalTimeSlotsPersistence().findAll() in your CalTimeSlotsImpl class

0
votes

Try this way

public List<String> getTimeValues() throws SystemException {
    List<String> timeValues = new ArrayList<String>();
    for (CalTimeSlots obj : getCalTimeSlotses(0, getCalTimeSlotsesCount())) {
        timeValues.add(obj.getTimeValue());
    }
    return timeValues;
}