如何在Quarkus 3.0.0.CR2 / Hibernate ORM 6中使用DISCRIMINATOR策略实现多租户?

huangapple go评论59阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月19日 15:08:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定