I'm using this interface for sharding. I'm not using the ShardIdentifierProviderTemplate because during the delete/purge/purgeAll methods I need to filter only some shards.
My problem is initialize method, where I must populate the initial shards. Is there a way to use current the SessionFactory for execute a selecting query on database?
The example on documentation uses the "requestService" method of the ServiceManager class, but in the current version the parameters of the method are changed and I can't seem to use it.
Code:
public static class AnimalShardIdentifierProvider extends ShardIdentifierProviderTemplate {
...
@Override
protected Set<String> loadInitialShardNames(Properties properties, BuildContext buildContext) {
ServiceManager serviceManager = buildContext.getServiceManager();
SessionFactory sessionFactory = serviceManager.requestService(
HibernateSessionFactoryServiceProvider.class, buildContext);
Session session = sessionFactory.openSession();
try {
Criteria initialShardsCriteria = session.createCriteria(Animal.class);
initialShardsCriteria.setProjection( Projections.distinct(Property.forName("type")));
@SuppressWarnings("unchecked")
List<String> initialTypes = initialShardsCriteria.list();
return new HashSet<String>(initialTypes);
}
finally {
session.close();
}
}
}