支持JPA eclipselnk多租户

时间:2014-10-16 19:46:59

标签: jpa orm eclipselink multi-tenant

我正在运行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>

我喜欢为所有实体使用相同的多租户配置: 单表,鉴别器列是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>

感谢。

1 个答案:

答案 0 :(得分: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中提取http://git.eclipse.org/c/eclipselink/eclipselink.runtime.git/tree/jpa/org.eclipse.persistence.jpa/resource/org/eclipse/persistence/jpa

相关问题