【问题标题】:how to replace annotations with file configuration (with XML) in spring boot project如何在spring boot项目中用文件配置(使用XML)替换注释
【发布时间】:2018-07-04 01:49:09
【问题描述】:

我正在寻找用配置文件(也是存储库文件中的查询)替换 JPA 和休眠的注释(@Entity、@Table、@Id、@GenratedValue,...),但我没有知道该怎么做,我知道我需要另一个(除了 application.properties)XML 格式的配置文件,我的问题是我在哪里创建这个文件?它的内容是什么?


非常感谢您回复我。

现在我的项目如下所示: spring boot project

这是我的 orm.xml 文件:

<entity-mappings>
    <entity class="org.sid.dao.entity.Etudiant">
        <table name="etudiants">
            <attributes>
                <id name="id">
                    <genrated-value strategy="AUTO"></genrated-value>
                </id>
                <basic name="nom"></basic>
                <basic name="prenom"></basic>
                <many-to-one>
                    <Join-column name="id_formation">
                        <basic name="formation"></basic>
                    </Join-column>
                </many-to-one>
            </attributes>
        </table>
    </entity>
    <entity class="org.sid.dao.entity.Formation">
        <table name="formations">
            <attributes>
                <id name="id">
                    <genrated-value strategy="AUTO"></genrated-value>
                </id>
                <basic name="nom"></basic>
                <basic name="duree"></basic>
                <one-to-many mappedby="formation" fetch="lazy">
                    <basic name="etudiants"></basic>
                </one-to-many>
            </attributes>
        </table>
    </entity>
</entity-mappings>

但是JPA不会在数据库中创建表,我该怎么办?

【问题讨论】:

  • 在已经使用 Spring Boot 的情况下为什么要这样做?
  • 请不要这样做。
  • 我只是想知道一个新的方法来做它,当我们将配置与看起来易于维护的类分开时,有人知道怎么做吗?
  • JPA 文档广泛可用,其中一些给出了注释的等效 XML 示例,例如 datanucleus.org:15080/products/accessplatform_5_1/jpa/…

标签: hibernate jpa spring-boot


【解决方案1】:

JPA 规范要求 XML 映射位于类路径中:

可以指定名为 orm.xml 的对象/关系映射 XML 文件 在持久单元根目录的 META-INF 目录中或 引用的任何 jar 文件的 META-INF 目录 持久性.xml。

另外,或者另外,一个或多个映射文件可以是 由持久性单元的映射文件元素引用 元素。这些映射文件可能存在于类中的任何位置 小路。 — JPA 2.1 规范的第 8.2.1.6.2 节

查看 Hibernate 文档:

https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#bootstrap-jpa-xml-files

【讨论】:

  • 问题是:spring-boot项目中没有一个叫META-INF的目录我知道JAR或者WAR文件中有一个叫META-INF的目录,但是orm.xml文件在构建时调用,生成 JAR/WAR 后无法复制 META-INF 目录中的文件,所以我现在能做什么?并感谢您的回复。
  • 只要在 src/main/resources 中添加这个目录就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 2014-10-19
  • 1970-01-01
  • 2016-08-13
  • 2020-08-05
  • 1970-01-01
相关资源
最近更新 更多