【问题标题】:Support JPA eclipselnk multi-tenancy支持 JPA eclipselnk 多租户
【发布时间】:2014-10-16 19:46:59
【问题描述】:

我正在运行一个 JPA 应用程序,现在我想支持多租户。我喜欢使用 XML 而不是注释。

我有几个从 persistence.xml 引用的 orm.xml。

<entity-mappings 
    xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
    version="2.0">

    <package>mypackage</package>

    <entity class="Foo" />
    <entity class="Bar" />

</entity-mappings>

我喜欢对所有实体使用相同的多租户配置: 单表,discriminator 列是tenantUserId,context-property 是tenant.userId。

根据: https://wiki.eclipse.org/EclipseLink/Examples/JPA/EclipseLink-ORM.XML

<tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/> 

要不要把这行放在上面?我尝试按以下方式创建 eclipselink-orm.xml

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_1.xsd"
    version="2.1">

    <tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/>

    <persistence-unit-metadata> 
        <persistence-unit-defaults>
            <tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/> 
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>

根据架构,两者都是无效的。 eclipselink-orm.xml 放在哪里?

有没有办法说:所有实体都是多租户(单表)?我是否必须为所有实体一一指定它们?

<entity-mappings 
    xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
    version="2.0">

    <package>mypackage</package>

    <entity class="Foo" >
      <multi-tenant/>
    </entity>

    <entity class="Bar" >
      <multi-tenant/>
    </entity>

</entity-mappings>

谢谢。

【问题讨论】:

    标签: jpa orm eclipselink multi-tenant


    【解决方案1】:

    来自http://www.eclipse.org/eclipselink/documentation/2.5/solutions/multitenancy002.htm 您正在正确使用持久性单元默认值:

    <persistence-unit-metadata> 
        <persistence-unit-defaults>
            <tenant-discriminator-column name="tenantUserId" context-property="tenant.userId"/> 
        </persistence-unit-defaults>
    </persistence-unit-metadata>
    

    问题是您使用了错误的架构版本。 2.1 没有包含多租户特性,所以需要使用 2.5 的 xds,eclipselink_orm_2_5.xsd。这应该在 eclipselink.jar 中或从 git 中提取,正如 James 在这里描述的 http://git.eclipse.org/c/eclipselink/eclipselink.runtime.git/tree/jpa/org.eclipse.persistence.jpa/resource/org/eclipse/persistence/jpa

    【讨论】:

    • 谢谢。将版本更改为 2.5。
    • 我使用映射超类来定义类层次结构的多租户。有没有办法为层次结构中的一个类禁用多租户?谢谢。
    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2014-01-17
    • 2019-07-11
    相关资源
    最近更新 更多