英文:
How to do Multitenancy in Quarkus 3.0.0.CR2 / Hibernate ORM 6 with DISCRIMINATOR Strategy?
问题
I would like to use Discriminator strategy in Quarkus.
我想在Quarkus中使用Discriminator策略。
It was not available in 2.X due to hibernate version. So I used a specific filter and interceptor to implement this strategy.
由于Hibernate版本问题,2.X版本中不可用。因此,我使用了特定的过滤器和拦截器来实现这个策略。
Now that hibernate has been upgraded I've tried it.
现在Hibernate已经升级,我尝试过了。
But I didn't find a way to configure the Hibernate CurrentTenantIdentifierResolver.
但我没有找到配置Hibernate CurrentTenantIdentifierResolver的方法。
Without CurrentTenantIdentifierResolver property, I have this issue:
没有CurrentTenantIdentifierResolver属性,我遇到了这个问题:
2023-04-18 17:21:24,566 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (vert.x-eventloop-thread-0) HTTP Request to /api/v1/condominiums failed, error id: 23a9a92b-2494-421f-8b9f-884589308e83-1: org.hibernate.HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified
at org.hibernate.internal.SessionImpl.setUpMultitenancy(SessionImpl.java:276)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:259)
at org.hibernate.reactive.session.impl.ReactiveSessionImpl.<init>(ReactiveSessionImpl.java:168)
at org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl.lambda$openSession$1(MutinySessionFactoryImpl.java:112)
It seems that the FastBootEntityManagerFactoryBuilder does not configure the CurrentTenantIdentifierResolver in case we just use @TenantID annotation without other properties.
看起来FastBootEntityManagerFactoryBuilder在我们只使用@TenantID注解而没有其他属性的情况下没有配置CurrentTenantIdentifierResolver。
I've tried a workaround using quarkus.hibernate-orm.unsupported-properties:
我尝试了使用quarkus.hibernate-orm.unsupported-properties来解决问题:
quarkus.hibernate-orm.unsupported-properties."hibernate.multiTenancy" = DISCRIMINATOR
quarkus.hibernate-orm.unsupported-properties."hibernate.tenant_identifier_resolver" = com.digicopro.base.multitenancy.TenantIdentifierResolver
The new issue is that in this way my TenantIdentifierResolver that use @RequestScope does not have access to the RoutingContext.
新问题是,使用这种方式,我的TenantIdentifierResolver使用@RequestScope时无法访问RoutingContext。
import io.vertx.ext.web.RoutingContext;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.jboss.logging.Logger;
@RequestScoped
public class TenantIdentifierResolver implements CurrentTenantIdentifierResolver {
private static final Logger LOG = Logger.getLogger(TenantIdentifierResolver.class);
@Inject
RoutingContext context;
@Override
public String resolveCurrentTenantIdentifier() {
final String tenantId = context.get(TenantEntity.TENANT_ID_PROPERTY_NAME);
LOG.debugv("TenantId = {0}", tenantId);
return tenantId;
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
and it produces: java.lang.NullPointerException: Cannot invoke "io.vertx.ext.web.RoutingContext.get(String)" because "this.context" is null.
并且产生了:`java.lang.NullPointerException: 无法调用“io.vertx.ext.web.RoutingContext.get(String)”因为“this.context”为空。
Certainly some glue missing between quarkus CDI and hibernate config.
显然,quarkus CDI和Hibernate配置之间缺少一些连接。
Any documentation or other way to do it? Do you think it's a missing feature that should be reported and planned?
有文档或其他方法可以解决吗?您认为这是一个应该报告和计划的缺失功能吗?
英文:
I would like to use Discriminator strategy in Quarkus.
It was not available in 2.X due to hibernate version. So I used a specific filter and interceptor to implement this strategy.
Now that hibernate has been upgraded I've tried it.
But I didn't find a way to configure the Hibernate CurrentTenantIdentifierResolver.
Without CurrentTenantIdentifierResolver property, I have this issue:
2023-04-18 17:21:24,566 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (vert.x-eventloop-thread-0) HTTP Request to /api/v1/condominiums failed, error id: 23a9a92b-2494-421f-8b9f-884589308e83-1: org.hibernate.HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified
at org.hibernate.internal.SessionImpl.setUpMultitenancy(SessionImpl.java:276)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:259)
at org.hibernate.reactive.session.impl.ReactiveSessionImpl.<init>(ReactiveSessionImpl.java:168)
at org.hibernate.reactive.mutiny.impl.MutinySessionFactoryImpl.lambda$openSession$1(MutinySessionFactoryImpl.java:112)
It seems that the FastBootEntityManagerFactoryBuilder does not configure the CurrentTenantIdentifierResolver in case we just use @TenantID annotation without other properties.
I've tried a workaround using quarkus.hibernate-orm.unsupported-properties :
quarkus.hibernate-orm.unsupported-properties."hibernate.multiTenancy" = DISCRIMINATOR
quarkus.hibernate-orm.unsupported-properties."hibernate.tenant_identifier_resolver" = com.digicopro.base.multitenancy.TenantIdentifierResolver
The new issue is that in this way my TenantIdentifierResolver that use @RequestScope does not have access to the RoutingContext.
import io.vertx.ext.web.RoutingContext;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.jboss.logging.Logger;
@RequestScoped
public class TenantIdentifierResolver implements CurrentTenantIdentifierResolver {
private static final Logger LOG = Logger.getLogger(TenantIdentifierResolver.class);
@Inject
RoutingContext context;
@Override
public String resolveCurrentTenantIdentifier() {
final String tenantId = context.get(TenantEntity.TENANT_ID_PROPERTY_NAME);
LOG.debugv("TenantId = {0}", tenantId);
return tenantId;
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
and it produces : java.lang.NullPointerException: Cannot invoke "io.vertx.ext.web.RoutingContext.get(String)" because "this.context" is null.
Certainly some glue missing between quarkus CDI and hibernate config.
Any documentation or other way to do it ? Do you think it's a missing feature that should be reported and planned ?
答案1
得分: 0
Discriminator-based multitenancy is not implemented yet in Quarkus: https://github.com/quarkusio/quarkus/issues/32760
See also https://github.com/quarkusio/quarkus/discussions/32686#discussioncomment-5660392
英文:
Discriminator-based multitenancy is not implemented yet in Quarkus: https://github.com/quarkusio/quarkus/issues/32760
See also https://github.com/quarkusio/quarkus/discussions/32686#discussioncomment-5660392
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论