Announcement Announcement Module
Collapse
No announcement yet.
Deploying Spring & Hibernate3 to Tomcat: NullPointerException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deploying Spring & Hibernate3 to Tomcat: NullPointerException

    Hi, I'm currently trying to figure out how to deploy my first JSP/Spring/Hibernate project and am completely stumped. The way I've organised it is to create a project with my business logic and data access objects, which is put into a jar file (including config files) in my WEB-INF/lib directory. The JSP file then accesses a bean defined in this jar file, which sets up spring and returns a spring bean that accesses the data.

    JSP runs, and loads the bean from the jar file. Spring starts initializing, but fails when it tries to create the hibernate SessionFactory. The stack trace of the root cause exception begins:

    java.lang.NullPointerException
    org.hibernate.util.ConfigHelper.getResourceAsStrea m(ConfigHelper.java:144)
    org.hibernate.cfg.Environment.<clinit>(Environment .java:524)
    org.hibernate.cfg.Configuration.reset(Configuratio n.java:168)
    org.hibernate.cfg.Configuration.<init>(Configurati on.java:187)
    org.hibernate.cfg.Configuration.<init>(Configurati on.java:191)
    sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:85)

    Relevant parts of my bean configuration are:

    <beans [...]>

    <tx:annotation-driven transaction-manager="txManager" />

    <bean id="dataSource" [...]>[...]</bean>

    <bean
    id="sessionFactory" autowire="byName"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="mappingResources"><list>
    <value>meridian/experimental/hibernatetest/DayList.hbm.xml</value>
    <value>meridian/experimental/hibernatetest/DayListEntry.hbm.xml</value>
    <value>meridian/experimental/hibernatetest/DayListTask.hbm.xml</value>
    </list></property>
    <property name="hibernateProperties"><value>
    hibernate.dialect=org.hibernate.dialect.MySQLDiale ct
    </value></property>
    </bean>

    <bean id="txManager" autowire="byName"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager" />
    [...]
    </beans>

    The configuration works fine when I run it as a unit test from within the 'hibernatetest' project.

    The files "meridian/experimental/hibernatetest/*.hbm.xml" are located in the jar file, which is the same jar file the spring configuration file is being loaded from, so is clearly accessible. I've also tried putting them in subdirectories from the web application's directory, from the WEB-INF directory and from the WEB-INF/lib directory, so I can only assume the problem isn't that hibernate is looking for them as files rather than on the classpath.

    I'd really appreciate it if someone can tell me what I'm doing wrong here.

  • #2
    I've also tried putting them in WEB-INF/classes. Again, Spring is able to load its configuration from this location, and the documentation suggests hibernate should be able to get its, so I'm less & less certain that this is related to its ability to find its configuration files.

    Comment


    • #3
      hello

      works if you work without the jar(where are located the hbm files) ???

      The files "meridian/experimental/hibernatetest/*.hbm.xml" are located in the jar file
      ok

      but how you say to the application that they are located in the jar??
      maybe the trick is there

      regards

      Comment


      • #4
        Hi,
        try using mappingJarLocations on your SessionFactory...
        hth
        marco

        Comment


        • #5
          Neither approach has any effect. The jar file must already be on the classpath, because it is the same jar file that contains the spring configuration file, which is working without me specifying any extra information.

          I think the next step for me is probably downloading the hibernate source code and looking to see what's happening at ConfigHelper.java:144.

          Comment


          • #6
            Hmmm....

            stream = Environment.class.getClassLoader().getResourceAsSt ream( stripped );

            Only way that could throw a null pointer exception is if getClassLoader() returns null. But I just don't see how that can be. Hibernate certainly isn't being loaded by the bootstrap classloader.

            Still, looking at that function, control shouldn't even be getting to that line if it's finding the resource OK. Time to scratch my head a little more, I guess.

            Comment


            • #7
              Gotcha.

              Hibernate expects to be able to load its config files using the same class loader that loaded its own classes. I had installed it in my tomcat 'common/approved' library directory, which is loaded by the shared classloader, not one configured with the locations for loading things out of my web app.

              Move hibernate & spring from there to my web app's 'WEB-INF/lib' directory and everything works fine.

              Why it didn't work when I gave it the jar location, I'm not sure.

              Comment


              • #8
                hello,
                little late... found out that somehow mappingJarLocations works if you have a fixed path like file:// xxx/myjar.jar

                couldn't get it to work with classpath: , which could have made it more dynamic..
                was wondering if you came across a solution for that...
                i came by chance across a post of a guy who has extended LocalSessionFactory Bean to look for jars in different classloaders.....
                i m going thru testing his code (according to his post, works like a charm.)

                i will let you (and him) know how it turns out for me... as i am trying to handle a SessionFactory where i dont need to hardcode anything (of course, a little naming convention comes to the rescue

                regards
                marco

                Comment

                Working...
                X