1 2 Previous Next 19 Replies Latest reply on Sep 26, 2006 4:16 AM by bfo81 Go to original post
      • 15. Re: Seam component as converter with EntityManager/Persisten
        bfo81

        @c_eric_ray: See Chapter 9.4.2 in the reference ;).


        @all: I tried using SLSB as converters some weeks ago. Thanks to js8523's third post I now know why it doesn't work.

        However, JavaBean Seam Components do work. But you have to add an interceptor ( @Interceptors(SeamInterceptor.class) or @Intercept(InterceptionType.ALWAYS), and use a Seam Managed Persistence Context ( @In(create=true) private EntityManager em; plus the changes in Chapter 9.4.2)...

        The DVD example converter is IMO way too fat. Don't blow up your code ;).

        • 16. Re: Seam component as converter with EntityManager/Persisten
          bfo81

          There is something important I forgot to say in that conclusion above:

          MAKE IT SERIALIZABLE ;)

          I did not succeed in running a Seam component converter with an EntityManager in it without making the class serializable. And I tried all combinations of scopes and interceptors.

          But that is something which is important for all your Seam Component or EJBs. Everything needs to be serializable. And if your bean has properties that do not implement Serializable, make them transient! Just for the sake of completeness :).

          • 17. Re: Seam component as converter with EntityManager/Persisten
            bfo81

            Well, those converter beans are really weird...

            In the getAsObject method you might load entities over and over via the em. So I decided to cache them (I even have a method to check if something is dirty).

            Well this cache is a inner class with a HashMap. It works well, entities get filled into the cache and retrieved from there. BUT: Only during one page invocation.

            As soon as I enter a page later, the cache is empty... or let's say, it's even a different cache (I logged the hashcode). This is the same problem like with the EntityManager. It gets lost after first invocation. And the funny thing: I even don't know how it's being reconstructed. I added a debug message to the cache's constructor, but it gets only called once. The next times the cache gets instantiated it overrides my constructor. This is extremely weird ^^.

            However, to draw the conclusion. If you have a converter with some kind of property

            ...
            @Name("theConverter")
            ...
            public class TheConverter ... {
            
             private SomeThing something = new SomeThing();
            
             public Object getAsObject(...) {
             use(something);
            ...

            "something" will be a different instance for every page invocation. If you want it to be always the same, use the Singleton pattern, so that the property class manages its instance itself.

            ...
             //use(something); //Not like this!
             use(SomeThing.instance()); //But that way!
            
            public class SomeThing implements Serializable {
             private static SomeThing instance;
             public static SomeThing instance() {
             if (instance == null)
             instance = new SomeThing();
             return instance;
             }
             ....


            • 18. Re: Seam component as converter with EntityManager/Persisten

               


              However, JavaBean Seam Components do work. But you have to add an interceptor ( @Interceptors(SeamInterceptor.class) or @Intercept(InterceptionType.ALWAYS), and use a Seam Managed Persistence Context ( @In(create=true) private EntityManager em; plus the changes in Chapter 9.4.2)...


              Can you show some examples on the what the code looks like?
              Where should the seam managed EM go?
              I'm still a little confused on the structure of this design.

              • 19. Re: Seam component as converter with EntityManager/Persisten
                bfo81

                 

                @Scope(ScopeType.SESSION)
                @Name("whateverConverter")
                @Interceptors(SeamInterceptor.class)
                
                public class WhateverConveter implements Converter, Serializable {
                
                 @In(create=true)
                 private EntityManager em; //Hint: There needs to be an "em" in your components.xml, see 9.4.2 of the Seam reference
                
                 public String getAsString(....Object object) {
                
                 return ( (Whatever) object ).getId().toString();
                
                 }
                
                 public Object getAsObject(....String string) {
                
                 return em.find(Whatever.class, Long.valueOf(string));
                
                 }
                
                
                }


                This converter example would make it possible to use your Entity beans in select menus. As I said I even added caching (even with a possibility to mark the contained entities as dirty if changes happen) and a method to generate SelectItems (with the same cache). But that's just an idea of the nice things you can do with a converter.

                PS: I reconstructed my converter and the caching went from an inner class directly into my code. And now it works like a charm. Inner classes seam to make problems in Converters, keep that in mind if you ever want to do that.

                1 2 Previous Next