NullPointerException when calling addEntity on a JPAContainer

Hello,

I’m building a web application with Vaadin 7, Spring 4 and Hibernate 4. I’m using JPAContainer add-on for persistence and I’m getting a NullPointerException when calling addEntity on my container. The same container is set as a data source for a table and it works perfectly fine - the table is displayed properly with no exceptions.

SEVERE: Servlet.service() for servlet [VaadinServlet] in context with path [/erasmus-plus-system] threw exception [com.vaadin.server.ServiceException: java.lang.NullPointerException] with root cause java.lang.NullPointerException at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.vaadin.addon.jpacontainer.metadata.ClassMetadata.getPropertyValueFromField(ClassMetadata.java:201) at com.vaadin.addon.jpacontainer.metadata.ClassMetadata.getPropertyValue(ClassMetadata.java:165) at com.vaadin.addon.jpacontainer.metadata.ClassMetadata.getPropertyValue(ClassMetadata.java:343) at com.vaadin.addon.jpacontainer.JPAContainer.addEntity(JPAContainer.java:1115) at pl.edu.pw.mini.erasmus.ui.ErasmusPlusApplication.init(ErasmusPlusApplication.java:97) at com.vaadin.ui.UI.doInit(UI.java:639) at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222) at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74) at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41) at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402) at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) The container:

public class UserJPAContainer extends JPAContainer<User> {

    private static final long serialVersionUID = 1L;

    @PersistenceContext
    private EntityManager entityManager;
    
    private JPAEntityProvider<User> jpaEntityProvider;
    
    public UserJPAContainer() {
        super(User.class);
        
        jpaEntityProvider = new JPAEntityProvider<User>(User.class);
        setEntityProvider(jpaEntityProvider);
    }
    
    @PostConstruct
    public void init() {
        jpaEntityProvider.setEntityManager(entityManager);
    }

}

My entity provider:

[code]
@Transactional(propagation = Propagation.REQUIRED)
public class JPAEntityProvider extends CachingMutableLocalEntityProvider {

private static Logger LOG = LoggerFactory.getLogger(JPAEntityProvider.class);

public JPAEntityProvider(Class<T> entityclass) {
    super(entityclass);
    setTransactionsHandledByProvider(false);
}

@Override
public T updateEntity(T entity) {

    LOG.info("Updating entity started");
    long t = System.currentTimeMillis();

    T updated = super.updateEntity(entity);
    LOG.info("Updated: " + updated.toString());
    t = System.currentTimeMillis() - t;
    LOG.info("Updating entity took: " + t + "ms");

    return updated;
}

@Override
public T addEntity(T entity) {

    LOG.info("Adding entity started");
    long t = System.currentTimeMillis();

    T added = null;

    try {
        added = super.addEntity(entity);
        LOG.info("Added: " + added.toString());
    } catch (Throwable e) {
        LOG.error(e.getMessage());
    }

    t = System.currentTimeMillis() - t;
    LOG.info("Adding entity took: " + t + "ms");

    return added;
}

@Override
public void removeEntity(Object entityId) {

    super.removeEntity(entityId);
}

@Override
public void updateEntityProperty(Object entityId, String propertyName,
        Object propertyValue) throws IllegalArgumentException {

    LOG.info("Updating entity's property started");
    long t = System.currentTimeMillis();

    super.updateEntityProperty(entityId, propertyName, propertyValue);

    t = System.currentTimeMillis() - t;
    LOG.info("Updating entity's property took: " + t + "ms");
}

// @PostConstruct
public void init(EntityManager em) {
    setEntityManager(em);
    /*
     * The entity manager is transaction scoped, which means that the
     * entities will be automatically detached when the transaction is
     * closed. Therefore, we do not need to explicitly detach them.
     */
    setEntitiesDetached(false);
}

}
[/code]Relevant part of my applicationContext.xml:

<context:annotation-config />
    <context:spring-configured />
    
    <context:component-scan base-package="my.package" />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:META-INF/persistence/jdbc.properties" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence/spring-persistence.xml" />
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="packagesToScan" value="my.package.*" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="false" />
        <property name="generateDdl" value="true" />
        <property name="database" value="MYSQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <tx:annotation-driven />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
   
    <bean id="userJPAContainer" class="pl.edu.pw.mini.erasmus.container.UserJPAContainer" scope="prototype"/>

Application code:

[code]
private JPAContainer getContainer() {
if (container == null) {
container = (UserJPAContainer) applicationContext.getBean(“userJPAContainer”);
}
return container;
}

// Create table
Table table = new Table(“Users”);
table.setContainerDataSource(getContainer());
content.addComponent(table); // THIS WORKS FINE

// Add a new user
User user = new User();
user.setEmail(“email@example.com”);
user.setLogin(“login”);

    getContainer().addEntity(user); // THIS LINE RAISES THE EXCEPTION

[/code]The stack trace doesn’t seem helpful to me - I have no idea what the problem is. Maybe some of you had similar problems using Spring, Hibernate and JPAContainer and will be able to help? :slight_smile:

Thank you in advance!