Thanks again for your reply.
I explored a bit along the lines of what you suggested. The
Criteria/Criterion APIs are distinctly unhelpful in discerning what's
inside. :-/ Rather than resorting to Java Reflection, I decided to go
with the approach suggested in the code: create a session with just the
shards I need.
The ShardedSessionImpl has no API to set the shards directly. The
constructors don't allow specifying shards; they use all the shards in
the ShardedSessionFactoryImpl (a parameter). Chasing that upstream, it
appears that the only point where I have any control over the shards
included is when creating the ShardedConfiguration, which is not
convenient at all. Perhaps I'm missing something, but it seems that the
clearest solution is to add a setter to the ShardedSessionImpl.
Given that ShardedSessionImplementor has getShards(), symmetry dictated
that it would also specify the corresponding setter. As it turns out,
it was convenient to add two methods for setting shards:
* Specify the shards that the session will use.
* @param shards a list of Shards to use
void setShards(List<Shard> shards);
* Specify by ShardId the shards that the session will use.
* @param shardIds a list of ShardIds identifying the Shards to use
void setShardsById(List<ShardId> shardIds);
The latter uses shardIdListToShardList internally to set the shards list.
One might argue for putting these into ShardedSession instead, since
that is "The main runtime inteface between Java application and
Hibernate Shards." I'm open to opinions.
Having the ability to set the shards on the session allows me to do
"shard resolution" in the DAO methods, where the session is accessible
and the query parameters are readily examined. The downside is that I
had to create a shard-aware set of DAOs subclassed from my
hibernate-specific DAOs, then add that as another DAO variant in my
DAOFactory. But all-in-all that's a small price to pay for directing
queries to a single shard rather than dozens. :-)
I'm happy to contribute my changes if anyone finds this worthwhile.