【问题标题】:How to setup a portable persistence.xml file?如何设置可移植的 persistence.xml 文件?
【发布时间】:2014-11-21 04:50:08
【问题描述】:

我正在构建一个使用 h2 数据库的应用程序,我的 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="taskUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.github.tarrsalah.jersey.todo.app.model.Task</class>
        <properties>          
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:file:/home/tarrsalah/src/github.com/tarrsalah/jaxrs-todo-app/target/todo"/>
            <property name="javax.persistence.jdbc.user" value=""/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.password" value=""/>
        </properties>
    </persistence-unit>
</persistence>

如您所见,我在 javax.persistence.jdbc.url 属性中有一个指向本地 h2 数据库的硬链接,我想在 github 上分享我的代码。

如何在不更改 persistence.xml 配置文件的情况下使我的代码的克隆版本正常工作?

PS:我正在使用flyway进行数据库迁移)

【问题讨论】:

    标签: java maven jpa h2


    【解决方案1】:

    为了与他人共享此代码,您可以尝试使用内存中的 h2 数据库

    <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=1000"/>
    

    【讨论】:

    • flyway迁移怎么样?
    • AFAIK flyway 迁移在 h2 内存数据库中工作。看这里stackoverflow.com/questions/21019874/…
    • @Shailendar 使用 maven flyway 插件迁移成功,但似乎 eclipseLink 找不到它(我使用的是相同的 URL)。
    • 我必须使用 java API 进行迁移,感谢建议使用内存选项。
    猜你喜欢
    • 2021-07-25
    • 2011-04-20
    • 1970-01-01
    • 2010-12-19
    • 2011-03-13
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多