Anda dapat memperluas MappingMongoEntityInformation untuk mengganti getCollectionName(). Operasi repositori memanggil getCollectionName() pada setiap operasi. Saya berasumsi tenantId akan menjadi ThreadLocal
public class TenantThreadLocal extends ThreadLocal<String> {
private final static TenantThreadLocal instance = new TenantThreadLocal();
public static TenantThreadLocal instance() {
return instance;
}
}
Dan kelas yang diganti dengan konstruktor dihilangkan.
public class TenantMappingMongoEntityInformation<T, ID extends java.io.Serializable> extends MappingMongoEntityInformation<T, ID> {
@Override
public String getCollectionName() {
return TenantThreadLocal.instance().get() + super.getCollectionName();
}
}
Kemudian buat repositori Anda:
MongoPersistentEntity<YourObject> persistentEntity =
(MongoPersistentEntity<YourObject>)
mongoOperations.getConverter()
.getMappingContext()
.getPersistentEntity(YourObject.class);
MongoEntityInformation<YourObject, ObjectId> mongoEntityInformation =
new MappingMongoEntityInformation<YourObject, ObjectId>(persistentEntity);
CrudRepository<YourObject, ObjectId> repository =
new SimpleMongoRepository<YourObject, ObjectId>(mongoEntityInformation, mongoOperations);