Hibernate: How to map a *.hbm.xml file in a different folder


I have some *.hbm.xml files that are placed in the same folder at the hibernate.cfg.xml file. Now, I want to map some other *.hbm.xml files that are in a subfolder of this folder. How could I do it? Thanks!

here's part of my hibernate.cfg.xml:

        <session-factory name="MySessionFactory">
             <!-- some hibernate properties here --> 

             <!--This below works fine-->
             <mapping resource="A.hbm.xml"/>

             <!--This doesn't-->
             <mapping resource="/dir/B.hbm.xml"/>


This is part of my Ant file:

        <target name="generateHibernateSql">

        <taskdef name="SchemaExportTask"
                <pathelement location="${build.classes.main.dir}"/>
                <pathelement location="${base.configuration.hibernate.dir}"/>
                <path refid="build.classpath.lib"/>

here's my folder structure

   | hibernate.cfg.xml
   | A.hbm.xml
   |---| B.hbm.xml


I have first tried and failed as Maurice suggested, changed the line to

 <mapping resource="dir/B.hbm.xml"/>

still gives the same error:

Schema text failed: Could not parse mapping document from resource dir/B.hbm.xml

and then I went on to try adding into my schemaexpoert as Mark suggested. It then wouldn't even find my "A.hbm.xml" anymore. Giving off the error:

Schema text failed: Could not parse mapping document from resource A.hbm.xml

My SchemaExportTask now looks like:

            <fileset dir="${base.configuration.hibernate.dir}">
                <include name="**/*.hbm.xml"/>


The conclusion is that I was just really stupid. It had nothing to do with being in a different directory. I got confused because I was changing two things at once testing phase and then I blamed it all on the innocent "directory change". Sorry for wasting everyone's time.

If anyone is interested, here was what happened. I did some XSLT translation using a local DTD file, and specified the local DTD file with a relative path in my XSL file. But I put the generated hbm.xml files into a difference directory — hence SchemaExportTask cannot find the DTD file anymore and failed to parse the new hbm.xml files. And for some stupid reason, I thought the following completely different error messages meant the same thing…. Thanks a lot to Mark for reminding me that people wrote error messages for a good reason! Adding fileset still doesn't work now, but I now know to read error meassages…I'm sure I'll fix it soon. =.=''

Schema text failed: resource: B.hbm.xml not found
Schema text failed: Could not parse mapping document from resource dir/B.hbm.xml

Best Solution

For general Hibernate usage I agree with change Maurice suggested but thought I would give more of an explanation. Hibernate mapping files are loaded as resources from the classpath. As you have ${base.configuration.hibernate.dir} on your classpath you must give the path of the mapping file relative to this therefore the front / should be removed.

For using the schema export tool from ant the docs say that you should define the mapping files as a fileset rather than expect them to be found on the classpath. See