在 jar 中嵌入 hibernate hbm.xml 映射

发布于 2024-08-15 01:30:42 字数 1043 浏览 3 评论 0原文

是否可以将 hibernate 映射 hbm.xml 嵌入到 jar 中并避免在 applicationContext.xml 中手动引用

  <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

并将其指向 jar/etc?
Nhibernate 有这样一个选项来指向一个程序集,从那里它获取 hbm。
注释不是一个选项

编辑: 编辑: 我的目的是删除对 hbm 的手动引用,并指向 hibernate 可以从中获取它的通用位置

  <list>
    <value>
      com/.../Question.hbm.xml
    </value>
    <value>com/.../Users.hbm.xml</value>
    <value>
      com/.../Answers.hbm.xml
    </value>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥有 2024-08-22 01:30:42

还要澄清一下:您专门一起谈论 Spring 和 Hibernate,因为您显示的配置是 Spring 的 Hibernate 配置。 Spring的 LocalSessionFactoryBean 接受多种不同的方式来设置 Hibernate 映射文件的位置;您仅显示使用 mappingResources 参数,但还有 mappingLocations, mappingJarLocationsmappingDirectoryLocations

我认为对于您的示例,您可能需要使用 mappingDirectoryLocations 和只需将其指向 JAR 中的特定目录,例如:

<property name="mappingDirectoryLocations">
      <list>
        <value>
          com/…/domain/
        </value>
      </list>
</property>

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>
命比纸薄 2024-08-22 01:30:42

是的,你可以,默认情况下,Spring 在搜索映射文件时从类路径开始。因此,如果该 jar 位于您的类路径上,那么它应该会发现这些文件对于要包含在本地会话工厂 bean 中的 hibernate 映射文件没有问题。

因此,如果您的映射文件位于 my.spring.package 中并且名为 mapping.xml,则该路径

my/spring/package/mapping.xml

应该可以正常工作。

编辑

感谢您的评论,我将更新我的答案。

,您不能指向 jar...但是,您可以指向 jar 中的映射文件。

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.

情仇皆在手 2024-08-22 01:30:42

我使用 mappingJarLocations 属性,如下所示来提取给定 jar 中的所有 *hbm.xml 文件:


    <属性名称=“mappingJarLocations”>
        <列表>
            <值>WEB-INF/lib/my-lib.jar
            ...

顺便说一句,AnnotationSessionFactoryBean 与注释和映射文件配合得很好。

编辑:我重新阅读了其中一些帖子,我还想指出,您可以通过创建抽象 bean 定义来减少重复的内容,如下所示:


    <属性名称=“packagesToScan”>
        <列表>
            <值>com.foo.*.*
            <值>com.foo.*.*.*
            <值>com.foo.*.*.*.*
        
    



    <属性名称=“mappingJarLocations”>
    ...


    <属性名称=“mappingJarLocations”>
    ...

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">
    ...
守望孤独 2024-08-22 01:30:42

这解决了我的问题

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

this solved my problem

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

感情洁癖 2024-08-22 01:30:42

它对我有用:

<property name="mappingJarLocations">
    <list>
        <value>file:**/yourJarContainHbms.jar</value>
    </list>
</property>

It works for me:

<property name="mappingJarLocations">
    <list>
        <value>file:**/yourJarContainHbms.jar</value>
    </list>
</property>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文