1

Looking around StackOverflow I see answers like this one which suggest that @Produces/@Inject is the way to go unless one specifically needs a remote Bean. Furthermore, Adam Bien says DAO is dead, so what is the "proper" EE 7 way to @Inject an EntityManager? Currently I have this kind of code:

@Path("/userdb")
public class UserDBInterface {
    @PersistenceContext
    private EntityManager em;

    @GET
    @Produces("text/plain")
    @Transactional
    public String dbInteraction()
    {
        User user1 = new User();
        user1.setLogin("ken");
        user1.setSalt(JpaSecurityUtils.getSalt());
        user1.setPassword(JpaSecurityUtils.hashPassword("password", user1.getSalt()));
        em.persist(user1);
        return "Created User DB";
    }
}

However, this gives me this error:

javax.servlet.ServletException: javax.transaction.TransactionalException: Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback.

Now, what the previous-referenced answer and this one suggest a solution like:

public class Resource {
    @PersistenceContext
    @Produces
    EntityManager em;
}

I have actually placed that in my @WebListener implementation of ServletContextListener. Then my usage code becomes:

@Path("/userdb")
public class UserDBInterface {
    @Inject private EntityManager em;

    // ...etc as above
}

However, I now get an error:

javax.servlet.ServletException: A MultiException has 1 exceptions. They are: 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=EntityManager,parent=UserDBInterface,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1502799812)

If I delete the @Transactional the injection appears to work, but I get a:

javax.servlet.ServletException: javax.persistence.TransactionRequiredException

I'm sure I'm missing something simple!

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • In your specific example above you are seeing "Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit" - can you edit your question with the details of this exception? – NBW Aug 04 '14 at 08:29
  • @NBW, I was using GlassFish 4.0 with JAX-RS 2.0, but I read that that was a bit buggy, so I upgraded to GF 4.0.1-b3 and JAX-RS 2.5, and I updated my code to just do a direct `@PersistenceContext EntityManager em;` and it appears to work. I'm still a beginner (and perhaps the tools are still immature too) so it always seems quite random as to what works or not. – Ken Y-N Aug 05 '14 at 01:47
  • Ken glad it worked for you. GF4 is up to promoted build 10 now so you might be interested in that. I ran into some issues with builds < 8. They are marching on to the 4.1 release around the time of JavaOne this fall so each promoted build gets better. – NBW Aug 05 '14 at 02:21

1 Answers1

1

Adam Bien is a proponent of the ECB (Entity, Control, Boundary) pattern with Java EE 7. In that case your boundary layer is your JAX-RS endpoint, it would not inject a PersistenceManager, but would inject a (most likely @Stateless) EJB (the Control layer). The EJB would have its PersistenceContext injected @PersistenceContext. The EJB itself would be injected into the JAX-RS endpoint with @EJB.

NBW
  • 1,467
  • 2
  • 18
  • 27
  • As noted in my comment for the main question, that was the solution I stumbled upon. There seemed to be hints that the JAX-RS and the CDI interfere with each other when used within the same class. Thanks for explaining Adam Bien's stance - I really should buy his book! – Ken Y-N Aug 05 '14 at 01:52
  • Personally I am not a fan of using CDI to inject PersistenceContexts - as things stand with Java EE 7 I prefer to use EJB 3.1 beans (they offer among other things richer transactional capabilities) for my controller classes. Java EE 8 will bring further parity between CDI and EJB. Another good resource are the [Java EE examples on github] (https://github.com/javaee-samples/javaee7-samples) – NBW Aug 05 '14 at 02:25