Hibernate Multi-Tenancy with Separate Schemas: Issues with Data Retrieval
I'm integrating two systems and I can't seem to get I'm trying to implement a multi-tenancy solution using Hibernate with separate schemas for each tenant... I have set up my `hibernate.cfg.xml` file to include the tenant identifier and created a custom `CurrentTenantIdentifierResolver`. However, when I attempt to retrieve data for a specific tenant, I keep encountering an issue where the data from the default schema is being fetched instead. My configuration looks like this: ```xml <hibernate-configuration> <session-factory> <property name="hibernate.multiTenancy">SCHEMA</property> <property name="hibernate.tenant_identifier_resolver">com.example.TenantIdentifierResolver</property> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mydb</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.connection.password">password</property> </session-factory> </hibernate-configuration> ``` In my `TenantIdentifierResolver` class, I have: ```java public class TenantIdentifierResolver implements CurrentTenantIdentifierResolver { @Override public String resolveCurrentTenantIdentifier() { // Logic to get tenant identifier, for example from a ThreadLocal or context return TenantContext.getCurrentTenant(); // Returns "tenant1" or "tenant2" } @Override public boolean validateExistingCurrentSessions() { return true; } } ``` When I try to query the database using the following code: ```java Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); List<MyEntity> entities = session.createQuery("FROM MyEntity", MyEntity.class).list(); transaction.commit(); session.close(); ``` I expect to see data specific to the tenant, but I keep getting records from the default schema. I've verified that the tenant identifier is being correctly set, but it seems like the schema switching isn't working as expected. Any suggestions or insights on what might be going wrong? Also, I'm using Hibernate version 5.4.30.Final and PostgreSQL 12. My team is using Java for this desktop app. Thanks for any help you can provide!