-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: NullPointerException when using @PersistenceContext -(JPA)
PostPosted: Tue Dec 30, 2008 12:41 pm 
Newbie

Joined: Tue Dec 30, 2008 12:24 pm
Posts: 4
I have been working through a JBoss (5.0.0.CR2) example using the tutorial at http://www.netbeans.org/kb/55/ejb30-jbo ... Exercise_5 (however using Netbeans 6.5).

I have an entity set up and upon deployment it is creating the table on the database. My problem is when I try to use a SLSB as a facade to retrieve entities back from the database. The generated code is as follows:


Code:
@Stateless
public class CustomerFacade implements CustomerFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Customer customer) {
        em.persist(customer);
    }

    public void edit(Customer customer) {
        em.merge(customer);
    }

    public void remove(Customer customer) {
        em.remove(em.merge(customer));
    }

    public Customer find(Object id) {
        return em.find(Customer.class, id);
    }

    public List<Customer> findAll() {
        return em.createQuery("select object(o) from Customer as o").getResultList();
    }

}


Unfortunately when I run this code, I get a NullPointerException upon usage of em because the PersistenceContext isn't being injected (I presume). This I'm sure is a config error, however I have also tried
Code:
    @PersistenceUnit(unitName = "PrototypeCRMSystem-ejbPU")
    private EntityManagerFactory emf;

    private EntityManager em = emf.createEntityManager();
which gives the same problem when accessing emf.

My persitence.xml is as follows:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
  <persistence-unit name="PrototypeCRMSystem-ejbPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/OracleDS</jta-data-source>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.show_sql" value="true" />
    </properties>
  </persistence-unit>
</persistence>


Please can someone advise on which piece of configuration I have missed?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2009 11:03 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
That all looks OK (the first SLSB is correct; the other things you tried are wrong). Can you post the stack trace?

Also, JBoss AS 5.0 is out now (GA) so you might wanna switch to that ;)

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 01, 2009 11:52 pm 
Newbie

Joined: Mon Mar 31, 2008 12:50 am
Posts: 4
You might wanna try :

Code:
   @PersistenceContext(unitName="BiddingTest")
   private static EntityManager em;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 5:56 am 
Newbie

Joined: Tue Dec 30, 2008 12:24 pm
Posts: 4
Stack trace as follows:

Code:
java.lang.NullPointerException
   at InformationServices.CustomerFacade.find(CustomerFacade.java:53)
   at InformationServices.CustomerServicePojo.getCustomer(CustomerServicePojo.java:37)
   at InformationServices.CustomerServiceBean.getCustomer(CustomerServiceBean.java:22)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
   at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_936879140.invoke(InvocationContextInterceptor_z_fillMethod_936879140.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_936879140.invoke(InvocationContextInterceptor_z_setup_936879140.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
   at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:194)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptorv2.invoke(RoleBasedAuthorizationInterceptorv2.java:201)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:157)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:65)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:241)
   at org.jboss.ejb3.proxy.handler.session.SessionSpecProxyInvocationHandlerBase.invoke(SessionSpecProxyInvocationHandlerBase.java:125)
   at $Proxy340.getCustomer(Unknown Source)
   at InformationServices.CustomerWebService.getCustomer(CustomerWebService.java:31)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
   at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_936879140.invoke(InvocationContextInterceptor_z_fillMethod_936879140.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_936879140.invoke(InvocationContextInterceptor_z_setup_936879140.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
   at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptorv2.invoke(RoleBasedAuthorizationInterceptorv2.java:201)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:157)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:65)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:345)
   at org.jboss.ejb3.stateless.StatelessContainer.invokeEndpoint(StatelessContainer.java:729)
   at org.jboss.wsf.container.jboss50.invocation.InvocationHandlerEJB3.invoke(InvocationHandlerEJB3.java:96)
   at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:219)
   at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:476)
   at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:295)
   at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:205)
   at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:131)
   at org.jboss.wsf.stack.jbws.EndpointServlet.service(EndpointServlet.java:81)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:189)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:91)
   at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:92)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:325)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Thread.java:619)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 6:25 am 
Newbie

Joined: Tue Dec 30, 2008 12:24 pm
Posts: 4
One way of getting this working is to add the following properties to the persistence.xml file:

Code:
<property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/>
      <property name="jboss.entity.manager.factory.jndi.name" value="java:/Manager1Factory"/>


and then setting the EntityManager using a static initialiser as follows:
Code:
    private EntityManagerFactory emf;
    private EntityManager em;


    {
        try {
            emf = (EntityManagerFactory) (new InitialContext()).lookup("java:/Manager1Factory");
            em = emf.createEntityManager();
        } catch (Exception e) {
            //TODO
        }
    }


In the mean time, I tried upgrading to 5.0.0.GA and changing various other bits (including referenced hibernate JARs), none of which seemed to make a difference themselves, but eventually something did and now I can use the injected version (of course I only discovered that it works whilst trying to reproduced for a stack-trace above). I do have a "broken" ear and a working ear, eventually I'll find time to get to the bottom of the problem, especially if other people are interested.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 10:11 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
OK this definitely seems to be a problem of your PU not deploying properly. Check the server.log for evidence to that effect. There will likely be some error messages about inability to deploy a persistence unit. This is really more of a JBoss question now so you might wanna take this over to the JBoss user forum (post a link here and I will follow it there if I have time).

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2009 5:47 am 
Newbie

Joined: Tue Dec 30, 2008 12:24 pm
Posts: 4
yes and no - if the PU wasn't deployed correctly, then the entity tables wouldn't get created (they are) and it wouldn't work using the JNDI lookup. As I say, I tweaked something and now it works, but as far as I can see it's to do with JBoss not injecting the PU.

Posted at JBoss - http://www.jboss.com/index.html?module= ... c&t=148011

If I ever get to the bottom of this (which I should as I have one that works and one that doesn't - trial and error will get there), I'll post the answer here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.