Question

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>
Was it helpful?

Solution

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>

OTHER TIPS

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.

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

this solved my problem

 <build>   <sourceDirectory>src/main/java</sourceDirectory>  
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>

It works for me:

<property name="mappingJarLocations">
    <list>
        <value>file:**/yourJarContainHbms.jar</value>
    </list>
</property>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top