同时拥有JPA和Liquibase:如何处理它们的配置文件? [英] Having both JPA and Liquibase: what to do with their configuration files?

查看:94
本文介绍了同时拥有JPA和Liquibase:如何处理它们的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目仅从JPA开始,并且没有Spring.后来,我添加了Liquibase,并且持久性单元名称存在一些问题,因为必须具有一个持久性单元名称才能使用EntityManager.

My project started with JPA only and it doesn't have Spring. Later, I added Liquibase and I had some issues with the persistence unit name since it is necessary to have one to be able to use EntityManager.

entityManagerFactory = Persistence.createEntityManagerFactory("MyPU");

因此,为了能够继续使用Liquibase创建表并使用JPA持久存储到数据库中,尽管保留了相同的数据库配置,我仍然保留了persistence.xmlliquibase.properties文件.

So, to be able to continue with the tables creation with Liquibase and persisting into the database with JPA, I kept both persistence.xml and liquibase.properties files, despite contaning the same database configuration.

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2.xsd"
             version="2.2">
    <persistence-unit name="MyPU">
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&amp;useTimezone=true&amp;serverTimezone=UTC"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

changeLogFile=src/main/resources/META-INF/database/dbchangelog.xml
url=jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC
username=root
password=root

我看过liquibase-hibernate,但我不太了解它,但是它似乎用于生成diff文件,目前我不需要.

I've taken a look at liquibase-hibernate and I didn't understand it very well but it seems to be used to generate the diff files, which is not my need at the moment.

两个文件都需要吗?我可以做一些只有其中之一的事情吗?

Are both files necessary? Is there something I can do to have only one of them?

推荐答案

Liquibase没有直接方法从presistence.xml文件中读取url/用户名/密码信息. liquibase-hibernate扩展确实添加了将数据库与您的Java文件映射文件进行比较的支持,但没有更改Liquibase如何获取url/username/password.

Liquibase doesn't have a direct way to read the url/username/password information from the presistence.xml file. The liquibase-hibernate extension does add support for diff'ing a database with your java file mapping files, but doesn't change how Liquibase gets the url/username/password.

您说过您没有使用Spring,但是如果您仍在Web应用程序中,则可以使用

You said you were not using Spring, but if you are still in a web application, you can use the Liquibase servlet listener to run Liquibase which pulls the connection from a pre-configured datasource. JPA can pull from that same pre-configured datasource instead of re-defining the configuration as well.

否则,除非您需要执行一些自定义Java编码来解析persistence.xml文件并将其传递到Liquibase,否则您确实需要两个文件.

Otherwise, unless you want to do a bit of custom Java coding to parse the persistence.xml file and pass it into Liquibase, you do need both files.

为避免重复,您可以执行一些操作,例如在maven/gradle/任何设置中定义构建属性,并在persistence.xml源文件中包含<property name="javax.persistence.jdbc.url" value="${database.url}"/>,在liquibase.properties文件中包含url: ${database.url}.

To avoid the repetition you could do something like defining build properties in your maven/gradle/whatever setup and have <property name="javax.persistence.jdbc.url" value="${database.url}"/>in your persistence.xml source file, and url: ${database.url} in your liquibase.properties file.

这篇关于同时拥有JPA和Liquibase:如何处理它们的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆