我正在尝试使用多租户选项配置Hibernate SessionFactory,并使用自动模式创建模式,但是我卡在了这个错误上:
java.lang.NullPointerException
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:498)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1780)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
这是我的Spring配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource1"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="hibernate.multiTenancy">DATABASE</prop>
<prop key="hibernate.multi_tenant_connection_provider">org.springframework.webflow.samples.booking.SampleMultiTenantConnectionProvider</prop>
<prop key="hibernate.tenant_identifier_resolver">org.springframework.webflow.samples.booking.SampleCurrentTenantIdentifierResolver</prop>
</props>
</property>
</bean>
<!-- Deploys a in-memory "booking" datasource populated -->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:booking1" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:booking2" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
目前我只使用数据源1,而SampleConnectionProvider和TenantResolver始终返回相同的租户和数据源。但是当Schema Export运行时,会抛出NullPointerException。 提供给SuppliedConnectionProviderHelper的ConnectionProvider为null。 在使用多租户时似乎无法选择适当的ConnectionProvider。 SuppliedConnectionProviderHelper在SchemaExport上创建:
this.connectionHelper = new SuppliedConnectionProviderConnectionHelper(
serviceRegistry.getService( ConnectionProvider.class )
);
当使用多租户时,您是否可以使用hibernate.hbm2ddl.auto?已经在Hibernate文档中搜索,但没有找到任何信息。
任何帮助都将不胜感激!
最好的问候
João Simas