6 Replies Latest reply on May 23, 2008 11:52 PM by dan.j.allen

    EntityManager injection: @PersistenceContext or @In ??

    benmoore

      Hi,


      In my session beans, I can inject an an EntityManager instance using @PersistenceContext or @In. Both work, but what is the semantic difference? I realize @In uses Seam and @PersistenceContext goes directly through JPA.


      When would I use one instead of the other? Is one preferred?


      Thanks for any advice,
      Ben

        • 1. Re: EntityManager injection: @PersistenceContext or @In ??
          graben

          With @In you inject the seam entitymanager which is injected all over the conversation. for further informations read the manual.

          • 2. Re: EntityManager injection: @PersistenceContext or @In ??
            dan.j.allen

            I just finished writing an extremely detailed explanation of the difference between the two in the updated chapters 8 and 9 of Seam in Action. These chapters will go out with the MEAP in a week or so. I can try to summarize with a few short bullets:



            1. Seam is not involved in @PersistenceContext injection

            2. It is proxied to give you EL and Hibernate Search

            3. Right now manual flushing and Hibernate filters are not available

            4. There are complex rules for sharing amongst EJB components and JavaBean components cannot use it at all



            Again, I go through all of this in detail.

            • 3. Re: EntityManager injection: @PersistenceContext or @In ??
              infinity2heaven

              Dan -- I'm looking fwd for that revision as I'm disappointed with the coverage of this topic in the initial MEAP release.


              Personally, I prefer using @In as Seam adds many features on top of EJB3, does flushing/merging all by default without the need to override these settings, in most standard use cases. Most importantly, you won't get the infamous LazyInitializationException if use @In. I have't trried the extended Persistent Context in @PersistenContext though.

              • 4. Re: EntityManager injection: @PersistenceContext or @In ??
                benmoore

                Thanks, Dan. It isn't clear to me if the last 3 bullet points apply to @In or @PersistenceContext. Can you elaborate? I'll definitely be getting your book, even if I don't know what MEAP means.


                Thanks,
                Ben

                • 5. Re: EntityManager injection: @PersistenceContext or @In ??
                  dan.j.allen

                  You won't be disappointed with the new chapters (chapter 8 and 9). Also, I explain LIE and its circumstances in detail. You don't need @In to avoid LIE, you need a conversation-scoped persistence context. Seam's EntityManager is conversation-scoped by default. A container-managed persistence context is made conversation-scoped by using @PersistenceContext(EXTENDED) on SFSB.

                  • 6. Re: EntityManager injection: @PersistenceContext or @In ??
                    dan.j.allen

                    All of those bullet points pertain to @PersistenceContext injection. As you can see, you can make it work, but a Seam-managed persistence context is just inherently better.


                    MEAP = Manning Early Access Program (basically, a PDF of the chapters in rough format)