如何使用LocalContainerEntityManagerFactoryBean使用hbm2dll初始化多个数据库?
我在MultiTenancy中为每个租户使用不同的数据源。 我在ApplicationContext.xml中有一个id为“dataSource”的默认数据源,我使用“dataSource”的属性为每个租户生成一个数据源,但更改了url。我将它们添加到自定义的MapDataSourceLookup中。
我尝试使用LocalContainerEntityManagerFactoryBean配置JPA初始化。
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory" >
<property name="persistenceUnitManager" ref="pum"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${database.dialect}" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.multi_tenant_connection_provider" value-ref="multitenancyConnectionProvider"/>
<entry key="hibernate.tenant_identifier_resolver" value-ref="tenantResolver"/>
</map>
</property>
</bean>
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="dataSourceLookup" ref="dataSourceLookup"/>
<property name="persistenceXmlLocations">
<list>
<value>classpath:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"
id="dataSource">
<property name="driverClass" value="${database.driverClassName}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="idleConnectionTestPeriod" value="${database.idleConnectionTestPeriod}" />
<property name="idleMaxAge" value="${database.idleMaxAge}" />
<property name="maxConnectionsPerPartition" value="${database.maxConnectionsPerPartition}" />
<property name="minConnectionsPerPartition" value="${database.minConnectionsPerPartition}" />
<property name="partitionCount" value="${database.partitionCount}" />
<property name="acquireIncrement" value="${database.acquireIncrement}" />
<property name="statementsCacheSize" value="${database.statementsCacheSize}" />
<property name="releaseHelperThreads" value="${database.releaseHelperThreads}" />
</bean>
我有一个注入的自定义@component dataSourceLookup,它以这种方式创建不同的数据源。
//Add new datasource with configuration from "dataSource" and new url per tenant
BoneCPDataSource customDataSource = new BoneCPDataSource(defaultDataSource.getConfig());
customDataSource.setJdbcUrl(props.getProperty("database.url"));
addDataSource(tenantId, customDataSource);
logger.info("Configuring tenant: " + tenantId + " Properties: " + customDataSource.toString());
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<property name="hibernate.show_sql" value="true" />
<!-- Multi-tenancy properties -->
<property name="hibernate.multiTenancy" value="DATABASE" />
</properties>
</persistence-unit>
现在唯一正在创建的方案是默认数据库。但是没有创建指向其他数据库的数据源。当我尝试访问用户时会产生此错误,因为USER表不存在:
引起:org.hsqldb.HsqlException:用户缺少未找到的权限或对象:USER
答案 0 :(得分:0)
我必须使用自定义扩展类LocalContainerEntityManagerFactoryBean并实现方法postProcessEntityManagerFactory(EntityManagerFactory emf,PersistenceUnitInfo pui)来解决它。
在那个方法中,我循环不同的数据源,并在每次迭代中使用该数据源创建entitymanagerfactory,我知道它不是最好的,但如果你知道更好的解决方案,我想听:)。目前多租户中的hibernate不支持Schemaexport,这是我找到的解决方案。