java - JPA + hibernate + MySQL : How to propagate a schema into the connection?

标签 java mysql spring hibernate jpa

我的应用程序中有几个独立的组件。每个数据库架构中都有自己的数据模型:

@Entity
@Table(name = "sample")  // the table name is not unique among components (schemas)
public class SampleEntity1 { ... }

类比于 SampleEntity2

每个组件都有自己的persistence-unitX.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="unit1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <mapping-file>META-INF/orm-unit1.xml</mapping-file>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties>
        <class>com.example.model1.SampleEntity1</class>
    </persistence-unit>
</persistence>

映射文件 orm-unitX.xml 设置模式:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0" xmlns="http://java.sun.com/xml/ns/persistence/orm">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>schema1</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>

类推其他组件。

为了让它工作,我希望连接字符串没有数据库部分,例如:jdbc:mysql://127.0.0.1:3306

不幸的是,这样的设置会抛出异常:

java.sql.SQLException: No database selected

将数据库(模式)设置到连接字符串中(例如:jdbc:mysql://127.0.0.1:3306/myschema)不会引发异常,但不会按预期工作 - 仅使用一个模式并将具有相同表名的实体合并到一个数据库表中。

设置属性 hibernate.default_schema 也不走运。

有没有办法将 JPA 架构设置传播到与 Hibernate 和 MySQL 的数据库连接中?


我正在使用 Spring Data (Spring Boot Data-Jpa starter 2.1.5.RELEASE) 和 MySQL 8 - 应该不会有任何影响。

当使用 EclipseLink 而不是 Hibernate 时,整个场景都有效,所以问题似乎出在 Hibernate 中。

Here是一个示例项目。

最佳答案

对于 Hibernate 和 MySQL,您必须在 orm.xml 中使用目录而不是模式:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0" xmlns="http://java.sun.com/xml/ns/persistence/orm">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <catalog>schema1</catalog>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>

来自文档:http://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html

The schema attribute of the @Table annotation works only if the underlying database supports schemas (e.g. PostgreSQL).

Therefore, if you’re using MySQL or MariaDB, which do not support schemas natively (schemas being just an alias for catalog), you need to use the catalog attribute, and not the schema one.

关于java - JPA + hibernate + MySQL : How to propagate a schema into the connection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56269195/

相关文章:

java - 向 Spring 应用程序添加另一个上下文

java - 映射列表不与实体一起保存到数据库中

mysql - Django SET SQL_AUTO_IS_NULL = 0

php - 如何每天自动调用一个php脚本自动发送邮件

java - 构建这个并发问题的正确方法是什么? Spring 启动

java - 从不同的队列中删除一个对象而不迭代它们或使用硬编码

php - 从逗号分隔的字符串中删除重复项并计算字符串总数

java - Selenium IE WebDriver 仅在调试时有效

java - 如何在裸存储库中使用 JGit 暂存文件?

java - 使用 Guava 的 Maps#filterKeys() 是个坏主意吗?