0

Looking at this answer, it says:

If you don't want to use an Application Client Container and instead just run the application client class through a java command, injection won't be possible and you'll have to perform a JNDI lookup.

However, given that I am trying to inject a DAO bean like the example shown here, if I cannot do the automatic injecting, it means my application must manually do the JNDI lookup and all the transaction begin/end that I would get for free if the @EJB actually worked.

However, since everything is all within the same Eclipse EJB Project (it also failed with the same null handle when I had my client code in a Dynamic Web Project), surely there must be an easy way to get it all working? Can anyone suggest what I am doing wrong?

Finally, this article suggests that DAOs are not needed, but if I replace within my EJB:

 @EJB MyDao dao;

with the more direct:

@PersistenceContext private EntityManager em;

I still get the similar null value; is this the same injection failure problem?


NB: I have just noticed this answer:

This is a bug in Glassfish (apparently in the web services stack).

I am running v4.0 Build 89, which still has this bug? Does this mean I have to do all JPA actions the long-winded way?

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114

1 Answers1

0

I eventually found out that the problem/issue is that in order to use injection of the @PersistenceContext the class MUST be a bean itself. This is hinted at in the example on Wikipedia:

@Stateless 
public class CustomerService { 

  @PersistenceContext 
  private EntityManager entityManager; 

  public void addCustomer(Customer customer) { 
    entityManager.persist(customer); 
  }
}

I could delete this question, but perhaps leaving this answer might provide a hint to someone, or at least show them a minimal working example of EJB and JPA.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114