2014-10-16 60 views
0

我有一個JPA應用程序正在運行,現在我想支持多租戶。我喜歡使用XML而不是註釋。支持JPA eclipselnk多租戶

我有幾個從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,上下文屬性是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

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

+0

謝謝。將版本更改爲2.5。 – eastwater 2014-10-17 18:56:09

+0

我使用了mapped-superclass來爲類層次結構定義多租戶。有沒有辦法爲層次結構中的一個類禁用multitenant?謝謝。 – eastwater 2014-10-17 19:17:54