-
BackgroundCustomTenantResolver.java @PersistenceUnitExtension
@RequestScoped
public class CustomTenantResolver implements TenantResolver {
@Override
public String getDefaultTenantId() {
return TenantUtil.NONE.toString();
}
@Override
public String resolveTenantId() {
var tenantId = TenantUtil.getTenantId().toString();
System.out.println("resolve tenantId " + tenantId);
return tenantId;
}
} TenantUtil public class TenantUtil {
private static final ThreadLocal<Serializable> tl = new ThreadLocal<>();
public static final Serializable ROOT = "-1";
public static final Serializable NONE = "-999";
public static void clear() {
tl.remove();
}
public static Serializable getTenantId() {
return tl.get();
}
private static void setTenantId(Serializable tenantId) {
tl.set(tenantId);
}
} TenantFilter @Priority(0)
@Provider
public class TenantFilter implements ContainerRequestFilter, ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
String tenantId = requestContext.getUriInfo().getQueryParameters().getFirst("tenantId");
TenantUtil.setTenantId(Objects.requireNonNullElse(tenantId, TenantUtil.NONE));
}
@Override
public void filter(
ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
TenantUtil.clear();
}
} RoleResource @Path("/role")
public class RoleResource {
@Inject RoleRepository roleRepository;
@GET
@Path("list")
public List<SysRole> list() {
return roleRepository.select().fetch();
}
@GET
@Path("test")
public void test() {
TenantUtil.setTenantId("123");
System.out.println(roleRepository.select().fetch());
TenantUtil.setTenantId("456");
System.out.println(roleRepository.select().fetch());
}
} Expected
[{"id":"1", "tenantId": "123"}]
[{"id":"2", "tenantId": "456"}] Problem
The |
Beta Was this translation helpful? Give feedback.
Answered by
yrodiere
Aug 28, 2023
Replies: 1 comment 3 replies
-
@yrodiere Hi, can you give me some suggestion about this? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, already did: https://stackoverflow.com/questions/76985783/how-to-query-from-multiple-tenants-of-hibernate6-in-a-single-request/76990686#76990686
Please don't duplicate your questions >_< Asking just here or just on stackoverflow would be enough.