5 Replies Latest reply on May 23, 2005 1:03 PM by luca.mandorlo

    getSession

    Shawn Garbett Newbie

      When writing an EJB that uses Hibernate I wrote the following:

      ...
      public class SomeEJB implements SomeEJBLocal, SomeEJBRemote
      {
       private static final String jndiName="java:hibernate/TestSessionFactory";
      
       public void doSomeHibernate()
       {
       Session session = HibernateContext.getSession(jndiName);
      ...
      


      I cringe at the thought that if naming ever changed one would have to update all of these strings referenced. One could put the strings in a properties file, or a single file class in that project. If it's mispelled it also doesn't get checked at compile time. The code of EJB usage looks so much neater:

       InitialContext ctx = new InitialContext();
       SomeEJB service=(SomeEJB)ctx.lookup(SomeEJB.class.getName());
      


      Is there someway to do something like this for Hibernate in JBoss so that it's a compile time check?

        • 1. Re: getSession
          beyarecords Novice

          Shawn,
          on a slightly different note, I saw an earlier post of yours where you changed the hbm.xml files DTD from 2 to 3. Can you please tell me how your hibernate.har and jboss-hibernate.deplayer is setup as I can't get my hibernate.har to deploy at all.

          I have changed all mapping files and class files to reflect the changes required for hibernate3 to work, but whe it comes to starting Jboss hibernate.har doesn't deploy at all. Any ideas?

          many thanks

          beya

          • 2. Re: getSession
            Shawn Garbett Newbie

            There are two other issues bouncing around about Hibernate3. The configuration as currently packaged ends up with two different copies of the Hibernate jar files. Delete one, and put the other in in the common lib directory.

            The other issue is more complicated and involves the fact the Hibernate opens the session with no auto flush. One has to flush explicitly.

            I'll reply in full tomorrow.

            Shawn

            • 3. Re: getSession
              beyarecords Novice

              Shawn,
              many thanks.

              beya

              • 4. Re: getSession
                Scott Stark Master

                Use a resource-ref to avoid hard-coded jndi names.

                • 5. Re: getSession
                  luca.mandorlo Newbie

                   

                  "scott.stark@jboss.org" wrote:
                  Use a resource-ref to avoid hard-coded jndi names.


                  I try to use a resource-ref for accesing to java:/hibernate/SpikeSessionFactory called hibernate.

                  Deployment is ok, but SessionFactory object in null again!!!

                  this i the code:

                  InitialContext context = null;
                   context = new InitialContext();
                   SessionFactory factory = (SessionFactory)context.lookup("java:comp/env/hibernate");
                   Session hsession = factory.openSession();


                  and in ejb-jar.xml

                  <ejb-jar>
                   <enterprise-beans>
                   <session>
                   <description>Timer Example session bean</description>
                   <display-name>Timer Example SessionBean</display-name>
                   <ejb-name>TimerExample</ejb-name>
                   <home>it.fastweb.spike.timer.TimerExampleHome</home>
                   <remote>it.fastweb.spike.timer.TimerExample</remote>
                   <ejb-class>it.fastweb.spike.timer.TimerExampleBean</ejb-class>
                   <session-type>Stateless</session-type>
                   <transaction-type>Container</transaction-type>
                   <resource-ref>
                   <res-ref-name>hibernate</res-ref-name>
                   <res-type>org.hibernate.SessionFactory</res-type>
                   <res-auth>Container</res-auth>
                   </resource-ref>
                   </session>
                   </enterprise-beans>
                  
                   <assembly-descriptor>
                  
                   <container-transaction>
                   <method>
                   <ejb-name>TimerExample</ejb-name>
                   <method-name>*</method-name>
                   </method>
                   <trans-attribute>Required</trans-attribute>
                   </container-transaction>
                  
                   </assembly-descriptor>
                  </ejb-jar>



                  in jboss.xml


                  <resource-managers>
                   <resource-manager>
                   <res-name>hibernate</res-name>
                   <res-jndi-name>java:/hibernate/SpikeSessionFactory</res-jndi-name>
                   </resource-manager>
                   </resource-managers>


                  Why the SessionFactory is null??? i'm gone crazy!!!