Is it possible to embed the hibernate mapping hbm.xml’s to the jar and avoid manual reference in applicationContext.xml like
Yes, you can, by default Spring goes from the class path when search for mapping files. So if the jar is on your class path, it should find the files no problem for the hibernate mapping files to be included in your local session factory bean.
So if your mapping file is in my.spring.package and is called mapping.xml, the path
my/spring/package/mapping.xml
should work just fine.
EDIT
Thanks for the comment, I will update my answer.
NO, you cannot point at a jar... but YES, you may point to mapping files in the jar.
Just to clarify, as well: you're specifically talking about Spring and Hibernate together, since the configuration you're showing is Spring's configuration of Hibernate. Spring's LocalSessionFactoryBean accepts a multitude of different ways to set the location of your Hibernate mapping files; you only show using the mappingResources parameter, but there's also mappingLocations, mappingJarLocations, and mappingDirectoryLocations.
I'd think that for your example, you might want to use mappingDirectoryLocations and just point it to a specific directory within your JAR, such as:
<property name="mappingDirectoryLocations">
<list>
<value>
com/…/domain/
</value>
</list>
</property>
this solved my problem
<build> <sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
I use the mappingJarLocations attribute like so to pull in all *hbm.xml files in a given jar:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="mappingJarLocations"> <list> <value>WEB-INF/lib/my-lib.jar</value> ...
AnnotationSessionFactoryBean plays nice with annotations and mapping files, btw.
Edit: I re-read some of these posts and I also want to point out that you can cut down on repetitive stuff by creating abstract bean definitions like so:
<bean id="annotatedClassList" abstract="true"> <property name="packagesToScan"> <list> <value>com.foo.*.*</value> <value>com.foo.*.*.*</value> <value>com.foo.*.*.*.*</value> </list> </property> </bean> <bean id="writingSessionFactory" parent="annotatedClassList" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="mappingJarLocations"> ... <bean id="readingSessionFactory" parent="annotatedClassList" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="mappingJarLocations"> ...
It works for me:
<property name="mappingJarLocations">
<list>
<value>file:**/yourJarContainHbms.jar</value>
</list>
</property>