views:

216

answers:

3

Is it possible to embed the hibernate mapping hbm.xml’s to the jar and avoid manual reference in applicationContext.xml like

  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
      <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">
          org.hibernate.dialect.MySQLDialect
        </prop>
      </props>
    </property>
    <property name="mappingResources">
      <list>
        <value>
          com/…/domain/Question.hbm.xml

and point it to a jar/etc?
Nhibernate has such an option to point to an assembly, from where it picks ups the hbm’s.
Annotations are not an option

Edit: Edit: My intention is to remove manual reference to hbm’s and point to a generic location from where hibernate can pick it up

  <list>
    <value>
      com/.../Question.hbm.xml
    </value>
    <value>com/.../Users.hbm.xml</value>
    <value>
      com/.../Answers.hbm.xml
    </value>
+2  A: 

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.

Zoidberg
I don't think that's what OP is asking. He's asking whether it's possible to not list mapping file names at all in `sessionFactory` configuration.
ChssPly76
+3  A: 

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>
delfuego
Yep, so many options... by default spring interprets every path from the classpath... which if you think about it makes sense. Nice answer, good elaboration on my own.
Zoidberg
+1  A: 

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">
    ...
zznate