Announcement Announcement Module
Collapse
No announcement yet.
mappingDirectoryLocations problem with jar files Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • mappingDirectoryLocations problem with jar files

    I used following xml to configure my Hibernate SessionFactory:
    ..
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingDirectoryLocations">
    <list>
    <value>mypackage/types</value>
    <value>mypackage/bo</value>
    </list>
    </property>
    etc...

    This works well, when the mapping files are in the filesystem, but if I pack my hibernate mapping files in a jar, I get the following exception:

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at mypackage.MyClass.main(MyClass.java:58)
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [local-config.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: class path resource [mypackage/types] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/myproject/myjar.jar!/mypackage/types
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapab
    leBeanFactory.java:365)
    etc..

    I was able to solve this problem by indicating every individual mapping file using the mappingResources property. However, this is quite cumbersome, since it requires the configuration file to be adapted every time I add, delete or rename a mapping file.

    My question is whether there is a way to still use the mappingDirectoryLocation property when the files are packed in a jar?


    thanks,

    Cor

  • #2
    AFAIK, you can't because Spring actually delegates the resources to Hibernate which doesn't support reading directories from within a jar.

    Comment


    • #3
      sure it does!

      Code:
      	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="mappingJarLocations">
      			<list>
      				<value>/WEB-INF/lib/myjar.jar</value>
      			</list> 
      		</property>
      
      		...
      
      	</bean>
      that works for me to load hibernate files. it will search the whole jar for hibernate config files...

      Comment


      • #4
        Right but does that work when the entier war is packed (the my.jar is in WEB-INF/lib inside another jar)?

        Comment


        • #5
          mappingDirectoryLocations needs to point to actual directories on the filesystem somewhere (whether static, or expanded at runtime by the container).

          This is because all Spring does is build File objects from the paths, verify that they are directories, and pass them to Hibernate, which is expecting Files.

          No getting around it...

          As for mappingJarLocations, same deal basically. If the JAR ends up sitting on the filesystem somewhere so it can be pointed to, that will work. So what this means is that if you have JARs in a war file, in many containers this will work because the container will actually expand out the WAR file contents. But a container is perfectly within its rights to access everything from inside the packed WAR. Mind you, in practice, I've only seen this for simple resources (class files, regular files, etc), for JARs in WARs most containers seem to put them somewhere temporary, which I guess makes access easier.

          Regards,

          Comment

          Working...
          X