3 Replies Latest reply on Apr 12, 2010 5:39 AM by gaborj

    Help with Hibernate on Seam

      Hi, i'm learning how to work with seam framework.


      I need to use an hibernate configuration file (somefile.cfg.xml and mapping.hbm.xml) instead default JPA like this:


      Default:


      @In
      EntityManager manager;



      what i want:




      @In
      SessionFactory factory;
      





      and factory should be initialized (automatic if is possible) using previous somefile.cfg.xml that i mentioned. I need the SessionFactory object injected.


      I have tried many thing, and each one gives me a different error. I think i must change components.xml replacing


      <persistence:managed-persistence-context .../>
      <persistence:entity-manager-factory .../>
      



      with this:


      <persistence:managed-hibernate-session .../>
      <persistence:hibernate-session-factory .../>
      



      and putting somefile.cfg.xml and mapping.hbm.xml in some place of project folder, but i ignore where to put both.


      Can i pass SessionFactory injected, and avoid manual initialization?
      is this possible? And how can i do it? Where somefile.cfg.xml and mapping.hbm.xml should be copied?


      Sorry if this is repeated question, but i have read so many versions that i prefer to ask directly.


      thanks

        • 1. Re: Help with Hibernate on Seam
          gaborj

          Of course it is possible, did you check the hibernate example included with the seam distribution examples directory? Anyway, you sure you need the SessionFactory? In most cases you simply inject hibernate Session and you do not care about the Factory managed by the framework, it is much simpler...

          • 2. Re: Help with Hibernate on Seam

            thanks Gabor...
            I need specifically the SessionFactory to instantiate another class from another API... I do not use directly the Session object... that API does it.
            I have review the hibernate example included with seam, and i saw following details:


            in components.xml:



            ...
            <persistence:hibernate-session-factory name="*hibernateSessionFactory*"/>
                
            <persistence:managed-hibernate-session name="*myprojectDatabase*"
                                                auto-create="true"/>
            ...




            in hibernate.cfg.xml:


            <session-factory name="java:/*myprojectDatabase*"> 
            ...




            and nothing else... i thinks that it´s enough to do this:



            @Name("somename")
            class XXX{
             @In
               private Session *myprojectDatabase*;
             @In
               private SessionFactory *hibernateSessionFactory*;
            
            }





            although i only need this:



            @Name("somename")
            class XXX{
             @In
               private SessionFactory *hibernateSessionFactory*;
            
             void somemethod(){
                classFromAPI(hibernateSessionFactory);
             }
            
            }



            I will test both...
            Although, one of many error that i got was that the hibernate.cfg.xml and the mapping.hbm.xml (specified inside the .cfg) were not found.
            Where should i copy both files? on resources dir?


            i am using Eclipse Ganymede, JBoss Tools Win32 3.0.0.GA-R200903141626-H5, Seam 2.1.1.GA and Jboss GA 4.2.2, if there is some related bug.


            I appreciate your concern... thanks again Gabor...
            Cheers...

            • 3. Re: Help with Hibernate on Seam
              gaborj

              Yes, first let Hibernate parse your settings and mappings. The .cfg.xml goes into resources in dev environment in this file you can refer to all your mapping files, look at some jBPM example e.g. the DVDStore... after successful bring-up you can retrieve reference to factory as



              • injection




              • Component.getInstance(<nameofComponentOrClassDef>)




              • directly from JNDI tree



              ...