2017-02-20 18 views
2

我正在使用java,jpa(eclipselink),mysql開發具有「共享數據庫/獨立架構」方法的多租戶Web應用程序。我的持久性文件看起來像:如何使用eclipselink在基於多架構的多租戶Web應用程序中添加缺失的列

<persistence-unit name="GroupBuilderPU" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
      <exclude-unlisted-classes>false</exclude-unlisted-classes> 
      <properties> 
       <property name="eclipselink.cache.shared.default" value="false"/> 
       <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?"/> 
       <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/> 
<--- Here goes other properties definition --> 
     </persistence-unit> 

現在,這裏是我的EntityMangerFactory和EntityManager的:

emfForTenant = Persistence.createEntityManagerFactory("GroupBuilderPU"); 
EntityManager em = emfForTenant.createEntityManager(); 
     em.setProperty("eclipselink.tenant-id", schemaNameAsTenantId); 

其工作正常,直到我在任何實體添加任何新的持久性列。

像我一個實體UserAccount在那裏我已經添加了一個新列「字符串rentalinfo」:

@Entity 
@Multitenant(MultitenantType.TABLE_PER_TENANT) 
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT) 
public class UserAccount implements Serializable { 
... 
private String rentalinfo;//Newly added column 
... 
} 

現在,經過這下面一行是給錯誤:

em.createQuery("SELECT ua FROM UserAccount ua").getResultList(); 

的錯誤是:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'RENTALINFO' in 'field list' 

那麼在此應用程序中添加新列(擴展表)的解決方案是什麼?蟑螂?

回答

1

您會收到此例外情況,因爲UserAccount表中不存在'RENTALINFO'列。在正常情況下,設置"create-or-extend-tables"將使EclipseLink爲您的現有表發佈ALTER,添加新列。然而,它會出現DDL生成不支持MultitenantType.TABLE_PER_TENANThttps://wiki.eclipse.org/EclipseLink/DesignDocs/Multi-Tenancy/TablePerTenant

Not supported:

  1. Schema generation will not be supported since it requires knowledge of all the tenants (schema's) and further to that, access provision must be set once the tables are created if using schema level table per tenant.

所以沒有ALTER和你的表沒有列。

作爲一個側面說明,您可以使用下面的持久性上的EclipseLink SQL日誌記錄功能:

<properties> 
    <property name="eclipselink.logging.level" value="ALL"/> 
    <property name="eclipselink.logging.level.sql" value="FINE"/> 
    <property name="eclipselink.logging.parameters" value="true"/> 
</properties> 

這樣,您就可以看到查詢的EclipseLink是(或者在這種情況下,是不是)執行。

+0

Yeap,我知道ddl世代不支持MultitenantType.Table_PER_TENANT。順便說一句,解決方案是使用任何數據庫遷移工具,如flyway,liquibase等我使用flyway的解決方案:) –

+0

@FarhanNazmul啊,我假定你正在尋找一個解決方案,涉及EclipseLink擴展你的桌子..那麼是的,解決辦法就是自己擴展桌子或者使用工具......很高興你能解決問題。 –

相關問題