You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@olingo.apache.org by Tim Wernick <ti...@wernick-net.de> on 2017/12/21 12:48:04 UTC

Olingo and Spring: lost databaseSession

Dear all,
I am currently developing an application which uses Spring Data JPA,
Olingo, Olingo JPA and Olingo Spring. The service starts up without
errors. Reading the entities works without an issue. Writing an entity
for the first time works fine but fails when there is a second post to
create the entity. The result returned by the service looks like this:

{
    "error": {
        "code": null,
        "message": {
            "lang": "en",
            "value": null
        }
    }
}

As far as I have seen the reason is a NullPointerException in
EntityManagerImpl.getActivePersistenceContext in this line:
            if(this.databaseSession.isServerSession()) {

The databaseSession is null at that time. Can someone please help me
with this?

Here's the setup of the project.

These are my gradle build dependencies (currently it's not cleaned up -
the annotation extension is not used):

    dependencies {
       compile 'org.apache.olingo:olingo-odata2-api:2.0.9',
                'org.apache.olingo:olingo-odata2-core:2.0.9',
                'org.apache.olingo:olingo-odata2-api-annotation:2.0.9',
               
    'org.apache.olingo:olingo-odata2-annotation-processor-api:2.0.9',
               
    'org.apache.olingo:olingo-odata2-annotation-processor-core:2.0.9',
                'org.apache.olingo:olingo-odata2-jpa-processor-core:2.0.9',
                'org.apache.olingo:olingo-odata2-spring:2.0.9',
                'org.springframework:spring-web:5.0.2.RELEASE',
                'org.springframework:spring-beans:5.0.2.RELEASE',
                'org.springframework:spring-context-support:5.0.2.RELEASE',
                'org.springframework.data:spring-data-jpa:2.0.2.RELEASE',
                'org.apache.cxf:cxf-rt-frontend-jaxrs:3.2.0',
                'org.quartz-scheduler:quartz:2.2.1',
                'com.sun.mail:javax.mail:1.5.1',
                'org.apache.commons:commons-lang3:3.7',
                'org.eclipse.persistence:eclipselink:2.5.1'
    }


The web.xml looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.5"
        xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <display-name>sf.tp</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/applicationContext.xml</param-value>
        </context-param>

        <resource-ref>
            <res-ref-name>jdbc/DefaultDB</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
        </resource-ref>

        <listener>
           
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <servlet>
            <display-name>CXFServlet</display-name>
            <servlet-name>CXFServlet</servlet-name>
           
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>CXFServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

    </web-app>


The applicationContext.xml looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jaxrs="http://cxf.apache.org/jaxrs"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee.xsd">


        <beans:bean id="entityManagerFactory"
           
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <beans:property name="persistenceUnitName" value="sf.tp" />
            <beans:property name="dataSource">
                <jee:jndi-lookup id="dataSource"
    jndi-name="java:comp/env/jdbc/DefaultDB" />
            </beans:property>
            <beans:property name="jpaVendorAdapter">
                <beans:bean
                   
    class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
    />
            </beans:property>
            <beans:property name="jpaDialect">
                <beans:bean
                   
    class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
            </beans:property>
            <beans:property name="jpaPropertyMap">
                <beans:props>
                    <beans:prop key="eclipselink.weaving">false</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>

        <beans:bean
            class="c.p.sf.tp.olingo.TPJobServiceFactory"
            id="odataJobServiceFactory">
            <beans:property name="entityManagerFactory"
    ref="entityManagerFactory" />
        </beans:bean>

        <beans:bean id="rootLocator"
            class="c.p.sf.tp.olingo.RootLocator">
            <beans:property name="serviceFactory"
    ref="odataJobServiceFactory" />
        </beans:bean>

        <beans:bean id="exceptionhandler"
           
    class="org.apache.olingo.odata2.core.rest.ODataExceptionMapperImpl" />

        <beans:bean id="provider"
           
    class="org.apache.olingo.odata2.core.rest.app.ODataApplication.MyProvider"
    />

        <jaxrs:server id="talentPoolService" address="/odata/v2">
            <jaxrs:serviceBeans>
                <ref bean="rootLocator" />
                <ref bean="exceptionhandler" />
                <ref bean="provider" />
            </jaxrs:serviceBeans>
        </jaxrs:server>
    </beans:beans>


The persistence.xml looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0"
        xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="sf.tp" transaction-type="RESOURCE_LOCAL">
           
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <class>c.p.sf.tp.data.JobSchedule</class>
            <class>c.p.sf.tp.data.JobConfig</class>
            <class>c.p.sf.tp.data.JobProperty</class>
            <class>c.p.sf.tp.data.JobHistory</class>
            <properties>
                <property name="eclipselink.ddl-generation"
    value="create-or-extend-tables" />
                <property name="eclipselink.logging.level" value="FINEST" />
                <property name="eclipselink-tenant.id" value="test" />
            </properties>
        </persistence-unit>
    </persistence>


The TPJobServiceFactory:

    package c.p.sf.tp.olingo;

    import javax.persistence.EntityManagerFactory;
    import javax.persistence.PersistenceUnit;

    import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
    import
    org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
    import
    org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

    public class TPJobServiceFactory extends ODataJPAServiceFactory {
        @PersistenceUnit
        private EntityManagerFactory em;
       
        private static final String PERSISTENCE_UNIT_NAME = "sf.tp";
       
        public void setEntityManagerFactory(EntityManagerFactory em) {
            this.em = em;
        }

        @Override
        public ODataJPAContext initializeODataJPAContext() throws
    ODataJPARuntimeException {
            final ODataJPAContext oDataJPAContext = getODataJPAContext();
            oDataJPAContext.setEntityManagerFactory(em);
            oDataJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
            return oDataJPAContext;
        }

    }

And last but not least the RootLocator:

    package c.p.sf.tp.olingo;

    import javax.ws.rs.Path;

    import org.apache.olingo.odata2.api.ODataServiceFactory;
    import org.apache.olingo.odata2.spring.OlingoRootLocator;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;

    @Path("/")
    public class RootLocator extends OlingoRootLocator {
        private static final Logger LOGGER =
    LoggerFactory.getLogger(RootLocator.class);

        @Override
        @Autowired
        public void setServiceFactory(ODataServiceFactory serviceFactory) {
            LOGGER.info("Setting Service Factory in RootLocator");
            LOGGER.info("ServiceFactory injected: " +
    serviceFactory.toString());
            super.setServiceFactory(serviceFactory);
        }

        public RootLocator() {
            super();
            LOGGER.info("RootLocator Constructor");
        }
    }

Many thanks in advance and kind regards



Tim