Glassfish JPA: Problems injecting EntityManager?

You get a NullPointerException because you are instantiating your BookService with a new() which is basically wrong - and nothing gets injected in the EJB. EJB are component that are managed by the container and should be obtained either via injection or with a lookup.

You get a NullPointerException because you are instantiating your BookService with a new() - which is basically wrong - and nothing gets injected in the EJB. EJB are component that are managed by the container and should be obtained either via injection or with a lookup. Here, while the JSP spec allows any code to be run in a scriplet, calling an EJB from a JSP is actually not really encouraged and JSPs don't support injection.In other words, you'll have to use a lookup: But you should call your EJB from a Servlet or a JSF Managed Bean (and your EJB could be injected in such components).

If you need some samples, have a look at the Java EE Code Samples & Apps. Update: See How do I access a Local EJB component from a POJO? In the EJB FAQ for more details on JNDI (especially the new portable global JNDI names defined by the EJB 3.1 specification).

Thank you very much. Very good explanation. I get: javax.naming.

NameNotFoundException: BookServiceBean not found? – Marcel Menz Mar 1 '10 at 22:49 @Marcel I was not totally sure about the JNDI name under which the bean would be available. Look at the GlassFish console, the JDNI name should be logged in there.

– Pascal Thivent Mar 1 '10 at 23:01 It worked like this: InitialContext(). Lookup("java:module/BookServiceBean"); Thank you again. Greets – Marcel Menz Mar 1 '10 at 23:06 @Marcel You're welcome.

Have fun with GlassFish :) – Pascal Thivent Mar 1 '10 at 23:21 Pascal, Thanks for this one too, I was having the same issue. I did not realize I could not create a new bean, I appreciate it. I used netbeans code generation to access mine, I am still not up to speed with the naming conventions that I can get that lookup name right without a little help.

– grantk Mar 1 '107 at 15:33.

You are instantiating the service bean directly, when you really need to be having the container inject it (via @EJB). This isn't supported in a JSP, though, so you'll have to switch to a servlet as well.

Thank you very much. If I use @EJB in a servlet I get a compilation error: The annotation @EJB is disallowed for this location. MMMMMMMMM Marcel – Marcel Menz Mar 1 '10 at 22:05 Could you mabye post a little example.

Sorry I am a novice – Marcel Menz Mar 1 '10 at 22:07 Ok. It has to be outside of the methods now I get a very nice Exception: – Marcel Menz Mar 1 '10 at 22:17 Exception: javax.naming. NamingException: Lookup failed for 'java:comp/env/controllers.

BookController/bs' in SerialContext Root exception is javax.naming. NamingException: Exception resolving Ejb for 'Remote ejb-ref name=controllers. BookController/bs,Remote 3.

X interface =beans. BookServiceBean,ejb-link=null,lookup=null,mappedName=,jndi-name=beans. BookServiceBean,refType=Session' .

– Marcel Menz Mar 1 '10 at 22:17 Actual (possibly internal) Remote JNDI name used for lookup is 'beans. BookServiceBean#beans. BookServiceBean' Root exception is javax.naming.

NamingException: Lookup failed for 'beans. BookServiceBean#beans. BookServiceBean' in SerialContext Root exception is javax.naming.

NameNotFoundException: beans. BookServiceBean#beans. BookServiceBean not found – Marcel Menz Mar 1 '10 at 22:18.

Public class BookController extends HttpServlet { private static final long serialVersionUID = 1L; @EJB BookServiceBean bs; public BookController() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Book be = new Book("Superman"); bs. CreateOrUpdate(b); } } This is what I do now. It leads to: javax.naming.

NamingException: Lookup failed for 'java:comp/env/controllers. BookController/bs' in SerialContext Root exception is javax.naming. NamingException: Exception resolving Ejb for 'Remote ejb-ref name=controllers.

BookController/bs,Remote 3. X interface =beans. BookServiceBean,ejb-link=null,lookup=null,mappedName=,jndi- name=beans.

BookServiceBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'beans. BookServiceBean#beans.

BookServiceBean' Root exception is javax.naming. NamingException: Lookup failed for 'beans. BookServiceBean#beans.

BookServiceBean' in SerialContext Root exception is javax.naming. NameNotFoundException: beans. BookServiceBean#beans.

BookServiceBean not found uppsssss.

1 Please post this as a new question (one question = one problem). – Pascal Thivent Mar 2 '10 at 1:11.

Try: @PersistenceContext(unitName = "sample") private EntityManager em.

1 That won't help as long as he is using the constructor. – whiskeysierra Mar 1 '10 at 22:05 Thank you. I changed this already and it doesn't seem to be the problem – Marcel Menz Mar 1 '10 at 22:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions