eastwater
eastwater

Reputation: 5640

Support JPA eclipselnk multi-tenancy

I have a JPA application running, and now I want to support multi-tenancy. I like to use XML instead of annotations.

I have a couple of orm.xml referenced from persistence.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>

I like to use the same multi-tenancy configuration for all entities: single-table, discriminator column is tenantUserId, context-property is tenant.userId.

According to: https://wiki.eclipse.org/EclipseLink/Examples/JPA/EclipseLink-ORM.XML

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

Whether to put the line above? I tried to create eclipselink-orm.xml as following

<?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>

Both are invalid according to the schema. Where to put eclipselink-orm.xml?

Is there a way to say that: all entities are multi-tenant(single table)? Do I have to specify them for all entities one by one?

<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>

Thanks.

Upvotes: 0

Views: 445

Answers (1)

Chris
Chris

Reputation: 21165

From http://www.eclipse.org/eclipselink/documentation/2.5/solutions/multitenancy002.htm you are using the persistence unit default correctly as:

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

The problem is you are using the wrong schema version. 2.1 did not include multitenant features, so you need to use the 2.5 xds, eclipselink_orm_2_5.xsd. This should be in the eclipselink.jar or pulled from git as James describes here http://git.eclipse.org/c/eclipselink/eclipselink.runtime.git/tree/jpa/org.eclipse.persistence.jpa/resource/org/eclipse/persistence/jpa

Upvotes: 1

Related Questions