3 Replies Latest reply on Jun 4, 2008 8:08 PM by mmichalek.mmichalek.micros-retail.com

    Injecting hibernate session

      I configured hibernate with seam and I am able to inject session object into my action classes which have reference from xhtml files.


      *MyBookAction class*
      @Name("action.myaction")
      public class MyBookAction {
        @In(value = "cwDataSession")
        Session session;
      public void save() {
        // persisetence logic.
        // session object is not null here.
      }




      *book.xhtml*
      .....
      <h:commandButton type="submit" value="Save" action="#{action.myaction.save}" />
      .....



      When I move the persistance logic to a DAO class the injection doesn't work anymore. Session object is always null.



      @Name("dao.hibernate_generic")
      public class GenericHibernateDAO<T, ID extends Serializable> implements GenericDAO<T, ID> {
      
      @In
      private Session session;
      
      public void save() {
        // If I point my xhtml file to this method. Session object is injected.
        // <h:commandButton type="submit" value="Save" action="#{dao.hibernate_generic.save}" />
      }
      



      Strangely if I change my xhtml to point to a method in DAO class, session is injected properly. But I don't want to reference my DAO in xhtml.


      I am using Seam 2.0.1GA and Hibernate 3.2.5GA. Deploying it is Jboss 4.2.2GA


      What am I doing wrong? Why do I see this strange behaviour?

        • 1. Re: Injecting hibernate session
          thejavafreak

          Did you inject your DAO from your Action class?

          • 2. Re: Injecting hibernate session

            No, currently I have a DAOFactory returning the DAO to my action class. But eventually I will inject it.


            MyDAOInterface myDAO = DAOFactory.getMyDAO();



            You are absolutely right. When I injected my dao into Action class session object is not null.


            So seam injections will happen only if seam is creating an object. I thought seam would inject even if I am creating object myself coz I am running in seam context. It makes sense to inject in this case but I am wondering if there is a way to create object myself and have injected objects?


            Something like


            MyObject myObject = SeamContext.createComponent(MyObject.class);


            • 3. Re: Injecting hibernate session
              mmichalek.mmichalek.micros-retail.com

              Have you tried

              org.jboss.seam.Component.getInstance(...)

              ?