[
https://hibernate.onjira.com/browse/HHH-7312?page=com.atlassian.jira.plug...
]
Stefan Schulze commented on HHH-7312:
-------------------------------------
Steve, obviously it is a bug that Hibernate can't create a entitymanager if all
necessary elements and information is in-place. The EntityManager knows its
SessionFactory, the SessionFactory knows about the current tenant, so the EntityManager
should be able to create a valid session. Possibly my solution is not "the right
one", but can you give me a hint how to solve this issue in a different way? I think
the correct way with respect to the javadoc of CurrentTenantIdentifierResolver would be
that EntityManagerImpl uses a dedicated CurrentSessionContext. Currently the EntityManager
doesn't use any CurrentSessionContext but manages the current session itself.
Furthermore it is not possible to define the tenant when using the current
EntityManagerImpl. You would have to use a custom EntityManager or have to inject a custom
SessionFactory into the EntityManagerImpl. Otherwise there is no way to come between
SessionFactory#withOptions() and SessionBuilder#openSession() in the current code.
It's a similar issue with SessionFactory#openSession() - in the current code this
method can't be used anymore in a tenant-aware scenario, although all information and
configuration is in-place.
Btw: I can't see any comment or something on the pull-requests. Just "sebersole
closed the pull request"...
JPA using schema based multi tenancy / resolveCurrentTenantIdentifier
not called, cannot create entity manager
--------------------------------------------------------------------------------------------------------------
Key: HHH-7312
URL:
https://hibernate.onjira.com/browse/HHH-7312
Project: Hibernate ORM
Issue Type: Bug
Components: core
Affects Versions: 4.1.2
Environment: Hibernate 4.1.2, using JPA
Reporter: Oriel Maute
Priority: Minor
Labels: jpa2, multi, schema-based, tenancy
Attachments: persistence.xml, SchemaBasedMultiTenancyTest.java,
SchemaBasedMultiTenantConnectionProvider.java, SchemaBasedTenantResolver.java
Original Estimate: 1h
Remaining Estimate: 1h
I tried to use db schema based multi tenancy with hibernate jpa / entitymanager. I use
the following properties in persistence.xml:
<property name="hibernate.multiTenancy" value="SCHEMA"/>
<property name="hibernate.tenant_identifier_resolver"
value="com.xoricon.persistence.bo.multitenancy.test.SchemaBasedTenantResolver"/>
<property name="hibernate.multi_tenant_connection_provider"
value="com.xoricon.persistence.bo.multitenancy.test.SchemaBasedMultiTenantConnectionProvider"/>
Both instances (SchemaBasedTenantResolver / SchemaBasedMultiTenantConnectionProvider)
will be instantiated by hibernate.
When i call:
EntityManagerFactory lEntityManagerFactory=
Persistence.createEntityManagerFactory("orm1");
EntityManager lManager = lEntityManagerFactory.createEntityManager();
I get:
System.out: SchemaBasedMultiTenantConnectionProvider.getAnyConnection()
System.out: SchemaBasedTenantResolver.<init>()
E
Time: 1,53
There was 1 error:
1)
test1(com.xoricon.persistence.bo.multitenancy.test.SchemaBasedMultiTenancyTest)org.hibernate.HibernateException:
SessionFactory configured for multi-tenancy, but no tenant identifier specified
at org.hibernate.internal.AbstractSessionImpl.<init>(AbstractSessionImpl.java:82)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:231)
at
org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1831)
at org.hibernate.ejb.EntityManagerImpl.getRawSession(EntityManagerImpl.java:121)
at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:98)
at
org.hibernate.ejb.AbstractEntityManagerImpl.setDefaultProperties(AbstractEntityManagerImpl.java:268)
at
org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:180)
at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:90)
at
org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:178)
at
org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:173)
at
com.xoricon.persistence.bo.multitenancy.test.SchemaBasedMultiTenancyTest.test1(SchemaBasedMultiTenancyTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
com.xoricon.persistence.bo.multitenancy.test.SchemaBasedMultiTenancyTest.main(SchemaBasedMultiTenancyTest.java:51)
FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1
In System.out "SchemaBasedTenantResolver.<init>()" shows, that my
CurrentTenantIdentifierResolver has been instantiated. But Hiberante did not call method
"public String resolveCurrentTenantIdentifier()".
I expect, that Hibernate uses the CurrentTenantIdentifierResolver to get the current
tenant id.
In JPA i cannot set the tenant on the Session, because the exception above occures before
Session has been created. I can access the Session only by EntityManager.getDelegate().
But as shown above, i cannot create an EntityManager instance.
--
This message is automatically generated by JIRA.
For more information on JIRA, see:
http://www.atlassian.com/software/jira