13 Replies Latest reply on Sep 30, 2005 7:20 AM by Emmanuel Bernard

    A problem with @Injected EntityManager

    Oleg Gorobets Newbie

      I examined examples from JBoss trailblazer and encounter an error. When trying to use "manager" variable (for example in manager.createQuery("from Author") -- look code below) it throws NullPointerException.

      manager value is null! Am I doing wrong? I'm new to EJB 3.0 so i think it is some problem with @Injected (in example it mentioned "@Inject", but Eclipse 3.1 with JBossIDE-1.5M2 returns an error on it allowing only @Injected). Can you suggest the way to solve this problem?

      package org.jboss.ejb3demo;
      import java.util.ArrayList;
      import java.util.List;
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.Query;
      import org.jboss.aspects.Injected;
      public @Stateless class AuthorsBean implements Authors {
       private EntityManager manager;
       public void addArticle(Author author, String title, String body) {
       public Author addAuthor(String name) {
       Author author=new Author();
       return author;
       public List<Author> getAllAuthors() {
       ArrayList<Author> authors = new ArrayList<Author>();
       if (manager==null) {
       System.err.print("--NULL-- ");
       return null;
       Query q = manager.createQuery("from Author");
       for (Object o:q.getResultList()) {
       authors.add((Author) o);
       return authors;

        • 1. Re: A problem with @Injected EntityManager
          Bill Burke Master

          You are using the wrong injection annotation. It is:


          What is the URL for the Trailblazer example in question? It should have been updated.



          • 2. Re: A problem with @Injected EntityManager
            Oleg Gorobets Newbie

            Thanks Bill! @PersistenceContext solves the problem, but here is one another, which already has been mentioned on this forum but with no solution. When I click on "Create articles" button I receive an error from Hibernate -- see below. I think there is a problem with manager.persist() function.

            The URL of trailblazer -- http://trailblazer.demo.jboss.com/IDETrail/ejb3/index.html

            org.hibernate.PersistentObjectException: detached entity passed to persist: org.jboss.ejb3demo.Author
             at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
             at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
             at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:531)
             at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:129)
             at org.jboss.ejb3.entity.InjectedEntityManager.persist(InjectedEntityManager.java:92)
             at org.jboss.ejb3demo.AuthorsBean.addArticle(AuthorsBean.java:20)
             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
             at java.lang.reflect.Method.invoke(Unknown Source)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:99)
             at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:33)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
             at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:183)
             at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
             at org.jboss.aspects.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:30)
             at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
             at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:79)
             at $Proxy119.addArticle(Unknown Source)
             at org.jboss.ejb3demo.web.AuthorServlet.createArticles(AuthorServlet.java:59)
             at org.jboss.ejb3demo.web.AuthorServlet.doPost(AuthorServlet.java:51)
             at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
             at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
             at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
             at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
             at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
             at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
             at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
             at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
             at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
             at java.lang.Thread.run(Unknown Source)

            • 3. Re: A problem with @Injected EntityManager
              Bill Burke Master

              we don't support injection into interceptors yet...

              • 4. Re: A problem with @Injected EntityManager
                Matthew Sell Newbie


                "bill.burke@jboss.com" wrote:
                we don't support injection into interceptors yet...


                Is it safe to assume that this tutorial won't work anymore? It's a great tutorial, even though the souce code displayed doesn't match the files in the download. It does help with learning the IDE.

                Any clues on what can be done to work around this problem? If not, then I can work on some other tutorial.


                • 5. Re: A problem with @Injected EntityManager
                  Matthew Sell Newbie

                  Just trying to work through the problem - getting the same error as the original poster.


                  It seems as if the first article and the first author *IS* being recorded to the Hypersonic database (I'm using the JBoss 4.0.3RC1 distro):

                  create table ARTICLES (articleId integer generated by default as identity (start with 1), title varchar(255), body varchar(255), authorId integer, primary key (articleId))
                  create table AUTHORS (authorId integer generated by default as identity (start with 1), name varchar(255), primary key (authorId))
                  alter table ARTICLES add constraint FKE566C23D843A2A25 foreign key (authorId) references AUTHORS
                  SET AUTOCOMMIT FALSE
                  INSERT INTO AUTHORS VALUES(1,'Marshall Culpepper')
                  INSERT INTO ARTICLES VALUES(1,'marshall''s 1st article','this is an article',1)

                  Gonna keep pluggin away - but this might help somebody much more experienced than I am at this.

                  • 6. Re: A problem with @Injected EntityManager
                    Matthew Sell Newbie

                    BTW: For my previous post, the Hypersonic log shows a minor modification I made to "Authors.java":

                    @Id (generate = GeneratorType.AUTO)
                    @Column (name = "authorId")
                    public int getAuthorId() {
                    return authorId;

                    I changed the column name for the AuthorID from "articleID" to "authorID".

                    While that doesn't change anything related to the problem, it does make the table structure make sense. Just a minor observation.

                    • 7. Re: A problem with @Injected EntityManager
                      Matthew Sell Newbie

                      OKay - looks like I have a fix for the tutorial. I would appreciate constructive criticism if these changes are not appropriate...

                      Problem #1: The column name in "Author.java" for the "AuthorID" is incorrectly set as "ArticleID". The relevant section should be:

                      @Id (generate = GeneratorType.AUTO)
                      @Column (name = "authorId")
                      public int getAuthorId() {
                      return authorId;

                      Problem #2: The order of persisting an Author to the db seems to be wrong. I fixed this by changing the "addAuthor" method in "AuthorsBean.java" to the following:

                      public Author addAuthor(String name) {
                      Author author = new Author();
                      return author;

                      Problem #3: When a new article is created, the author is requested for persistance to the db, not the new article. This is fixed by changing the "addArticle" method in "AuthorsBean.java" to the following:

                      public void addArticle(Author author, String title, String body)

                      Article newArticle = new Article();


                      Problem #4: With the above changes made, the "addArticle" method in "Author.java" can be simplified to the following:

                      public void addArticle (Article article)
                      if (articles == null)
                      articles = new ArrayList();


                      With these changes, the articles are listed properly after clicking on "Create Articles", and the SQL statements listed in the Hypersonic log appear to be correct. Hope this helps. I'm using this tutorial to learn how this all fits together, so if these "fixes" are not appropriate I would like to know.

                      • 8. Re: A problem with @Injected EntityManager
                        sumit_chawla Newbie

                        Grr, this was the problem I was also having, and I spent the better part of my weekend trying to get the JBoss IDE trailblazer tutorial to work (yeah, I'm a newbie), and as soon as it was working, my first google search showed up the fix you provided. :-(

                        The changes you mentioned are similar to what I had to do. I had to, of course, add the manager.persist(author) line when creating the Author, and I also added manager.persist(article) everytime a new article was created. This made the tutorial work. These were minor changes, but unfortunately the exceptions and error messages were not at all intuitive.

                        I sure hope the trailblazer IDE example is fixed soon.

                        • 9. Re: A problem with @Injected EntityManager
                          Jeff Newbie

                          Does anyone get a NameNotFoundException when the Servlet trys to lookup the Authors.class?

                          Context context = new InitialContext();
                          authorsBean = (Authors) context.lookup(Authors.class.getName());

                          • 10. Re: A problem with @Injected EntityManager
                            Alejandro Bernal Newbie

                            Thans for the information I have been able to deploy the trail tutorial byut when executing I'm getting a:

                            java.io.NotSerializableException: org.jboss.ejb3demo.Author error.

                            The authors are not getting listed and when clicking on the "create Articles" button raises the exception.

                            I have checked but can't find what might be the problem,

                            • 11. Re: A problem with @Injected EntityManager
                              C Chandraker Newbie

                              Try implementing java.io.Serializable to Author.java and Article.java

                              • 12. Re: A problem with @Injected EntityManager
                                Parveen Beniwal Newbie

                                Hi You must first merge(...) the detached instances and then persist them. And do not forget that you must call persist(...) on the instance returned by merge method.

                                public void addArticle(Author author, String title, String body)
                                author = manager.merge(author);

                                author.addArticle(title, body);

                                • 13. Re: A problem with @Injected EntityManager
                                  Emmanuel Bernard Master


                                  "parveenbeniwal" wrote:
                                  Hi You must first merge(...) the detached instances and then persist them. And do not forget that you must call persist(...) on the instance returned by merge method.

                                  Of course not!