How to avoid defining each Hibernate mapping in Spring

A better option than defining each mapping resource using the mappingResources property of LocalSessionFactoryBean is using mappingDirectoryLocations with something like the following, only needed once for all the system.

<bean id="sessionFactory">
 <property name="dataSource">
   <ref bean="dataSource">
   <property name="mappingDirectoryLocations">
   <property name="hibernateProperties">
     <ref bean="hibernateProperties">

But be careful, this doesn’t allow you to use classpath locations, they have to be in the filesystem. AFAIK there’s no way to use all mapping files in the classpath under some package, that would be the best solution, e.g. I wanna use all *.hbm.xml in the classpath under net/sf/oness

4 thoughts on “How to avoid defining each Hibernate mapping in Spring

  1. The fact that you can’t use classpath hurts a lot.

    The current solution doen’t work when doing war deployments under any os etc…

  2. Actually you can specify a classpath location for your mapping files using classpath: . Here is a snip from my applicationContext file:


    Works like a charm. Not sure where I first came across this.


  3. Yep, there is no way to ‘scan’ the classpath for resources or classes if it aint in the filesystem form.
    That’s too bad since getting an index out of a ZipFile/JarFile is easy…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s