Java – how to enable multi tenancy in Hibernate 4 using JPA?

It looks like my support for multi security has been added to hibernate and updated at least once since

Getting multi tenant meetings outside JPA seems rather trivial:

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

However, how do I enable hibernate in applications that use it through JPA? (if possible)

Thank you in advance

Solution

You can use persistence XML, as shown below:

<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 a multi tenant policy, hibernate is not required multi_ tenant_ connection_ provider.

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

More information about hibernate documentation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>