如何使用JPA在Hibernate 4中启用多租户? [英] How to enable multi-tenancy in Hibernate 4 with JPA?

查看:352
本文介绍了如何使用JPA在Hibernate 4中启用多租户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来好像支持多租户已被添加到休眠近6个月,自至少更新一次



在JPA之外获得多租户会话看起来相当简单:

  Session session = sessionFactory.withOptions()。tenantIdentifier(jboss).openSession(); 

但是如何在通过JPA使用hibernate的应用程序中启用它? (如果可能的话)。



在此先感谢。

可以通过persistence.xml中的属性来配置它: />
< property name =hibernate.multi_tenant_connection_providervalue =com.example.MyConnectionProvider/>
< property name =hibernate.tenant_identifier_resolvervalue =com.example.MyTenantIdResolver/>如果您使用SCHEMA作为多租户策略 hibernate.multi_tenant_connection_provider code>不需要。


您也可以在代码中设置这些属性,并将它们映射到 Persistence.createEntityManagerFactory()。在这种情况下,您可以传递一个对象实例,而不仅仅是一个类名。



更多信息在 Hibernate文档


It looks to me as though support for multi tenancy has been added to hibernate for nearly six months now and updated at least once since.

It looks fairly trivial to obtain a multi-tenant Session outside of JPA:

Session session = sessionFactory.withOptions().tenantIdentifier( "jboss" ).openSession();

But how would you enable it in an application that uses hibernate via JPA? (If possible).

Thanks in advance.

解决方案

You can configure it via properties in persistence.xml as follows:

<property name="hibernate.multiTenancy" value="DATABASE"/>
<property name="hibernate.multi_tenant_connection_provider" value="com.example.MyConnectionProvider" />
<property name="hibernate.tenant_identifier_resolver" value="com.example.MyTenantIdResolver" />

If you use SCHEMA as multi-tenancy strategy hibernate.multi_tenant_connection_provider is not needed.

You can also set these properties in your code and pass them in a map to Persistence.createEntityManagerFactory(). In this case you can pass an object instance, not just a class name.

More info in Hibernate documentation.

这篇关于如何使用JPA在Hibernate 4中启用多租户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆