JPA and JSF: right way of injecting EntityManager

前端 未结 2 1245
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 06:08

It has been some hours I\'m working on this but still I haven\'t figured out what is the right way of using JPA with JSF.

I have a session scoped managed bean that can d

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-21 06:37

    "someone tells that I have to inject the EM directly into my managed beans using the PersistenceContext annotation."

    If you look at the Java EE tutorial, that's exactly what they do

    @Singleton
    public class MyUserDatabaseEntityManager { 
        // declare a producer field 
        @Produces
        @UserDatabase
        @PersistenceContext
        private EntityManager em;
    }
    
    @ConversationScoped
    @Stateful
    public class RequestBean {
    
        @Inject
        @UserDatabase
        EntityManager em;
    

    This may not completely answer your question, but hope it helps.

提交回复
热议问题