2 Replies Latest reply on Aug 9, 2011 2:45 PM by Joshua Davis

    Seam with DAO

    Fatih Söylemez Newbie
      Hello,
      I have a few questions that i hope you can answer.
      I am developing a project with Seam JPA and Tomcat(i dont use ejb3).It is not a simple application that i will only use CRUD operations on database.
      And i like to use DAO pattern which makes code more readable,reusable etc.

      My first questions are,

         1. is it not good/useless to use service and dao layer with Seam? (Service,ServiceImpl,DAO,HibernateDAO)   


         2. if i use DAOs , do i have to use EntityManager inside them?What happens if i use EntityManager what happens if i dont?    



         3.    how can i inject Services/DAOs into the session? i was using Service,Repository annotations to declare them and Autowired
      annotations to get them from the session while using Spring.is there any similar way?   

      For example i have the following code,
      view plaincopy to clipboardprint?

      @Name("menuCategoryDao") 
      @Scope(ScopeType.EVENT) 
      @AutoCreate 
        public class MenuCategoryDAO extends AbstractDAO<MenuCategory, Long>{  
        } 

      but when i call MenuCategoryDAO from an Action class with @In annotation,it comes null.Do i have to make any other declarations in the components.xml or somewhere else?


      Can someone give me example of implementing DAOs without using EntityManager and using them
      into Action classes.
        • 1. Re: Seam with DAO
          santosh varma Newbie

          hi Fatih Söylemez,


          Did you find answers to your questions.Iam also searching similar kind of questions.can you share if you find them


          thanks

          • 2. Re: Seam with DAO
            Joshua Davis Expert

            Fatih Söylemez wrote on Mar 27, 2010 18:14:


            Hello,
            I have a few questions that i hope you can answer.
            I am developing a project with Seam JPA and Tomcat(i dont use ejb3).It is not a simple application that i will only use CRUD operations on database.
            And i like to use DAO pattern which makes code more readable,reusable etc.

            My first questions are,

               1. is it not good/useless to use service and dao layer with Seam? (Service,ServiceImpl,DAO,HibernateDAO)   


            You can use the DAO pattern in a Seam application, but you will find that your DAO implementations will be very thin if you use the Seam-managed EntityManager.   In many cases you can just get rid of the DAOs.



               2. if i use DAOs , do i have to use EntityManager inside them?What happens if i use EntityManager what happens if i dont?    


            Not sure what you mean by that.   If you use the Seam-managed EntityManager, you need to be aware of it's lifecylce, etc.





               3.    how can i inject Services/DAOs into the session? i was using Service,Repository annotations to declare them and Autowired
            annotations to get them from the session while using Spring.is there any similar way?   

            For example i have the following code,
            view plaincopy to clipboardprint?


             @Name("menuCategoryDao")  
             @Scope(ScopeType.EVENT)  
             @AutoCreate  
              public class MenuCategoryDAO extends AbstractDAO<MenuCategory, Long>{   
              }  
            



            but when i call MenuCategoryDAO from an Action class with @In annotation,it comes null.Do i have to make any other declarations in the components.xml or somewhere else?


            Can someone give me example of implementing DAOs without using EntityManager and using them
            into Action classes.


            That depends.  What are you going to use instead of EntityManager?   Why wouldn't you use the Seam-managed EntityManager?   It is very similar to how Spring does session-in-view with Hibernate sessions (actually, it's a little more sophisticated/correct).


            First, declare the Seam-managed EntityManager in components.xml:


                <transaction:ejb-transaction/>
            
                <persistence:managed-persistence-context name="entityManager"
                                                         auto-create="true"
                                                         persistence-unit-jndi-name="java:/EntityManagerFactories/mydb"/>
            



            A DAO in a non-EJB app is simply an application scoped bean that injects the Seam-managed EntityManager.


            @Name("myDao")
            @Scope(ScopeType.APPLICATION)
            public class MyDao
            {
            ...
                @In
                private EntityManager entityManager;
            ...
            }
            



            Seam will automatically inject appropriate EntityManager before every method call.  You can inject this bean into 'action' beans.