Original code section that incorrectly throws exception if id is not ShardEncodingIdentifierGenerator.
IdentifierGenerator idGenerator = shard.getSessionFactoryImplementor().getIdentifierGenerator(className);
if (idGenerator instanceof ShardEncodingIdentifierGenerator) {
return ((ShardEncodingIdentifierGenerator)idGenerator).extractShardId(getIdentifier(obj));
} else {
// TODO(tomislav): also use shard resolution strategy if it returns only 1 shard; throwthis error in config instead of here
thrownew HibernateException("Can not use virtual sharding with non-shard resolving id gen");
}
Updated complete method with added code to handle user generated id types.
public ShardId getShardIdForObject(Object obj, List<Shard> shardsToConsider) {
// TODO(maxr) optimize this by keeping an identity map of objects to shardId
Shard shard = getShardForObject(obj, shardsToConsider);
if(shard == null) {
returnnull;
} elseif (shard.getShardIds().size() == 1) {
return shard.getShardIds().iterator().next();
} else {
String className;
if (obj instanceof HibernateProxy) {
className = ((HibernateProxy)obj).getHibernateLazyInitializer().getPersistentClass().getName();
}
else {
className = obj.getClass().getName();
}
IdentifierGenerator idGenerator = shard.getSessionFactoryImplementor().getIdentifierGenerator(className);
if (idGenerator instanceof ShardEncodingIdentifierGenerator) {
return ((ShardEncodingIdentifierGenerator)idGenerator).extractShardId(getIdentifier(obj));
} else {
// ======== New ===========
List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
ShardResolutionStrategyDataImpl(obj.getClass(), getIdentifier(obj)));
if (shardIds!=null && shardIds.size() > 0) {
return shardIds.get(0);
}
// ========================
thrownew HibernateException("Can not resolve shard id using virtual sharding");
}
}
}
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
With formatting and new code section commented...
Original code section that incorrectly throws exception if id is not ShardEncodingIdentifierGenerator.
Updated complete method with added code to handle user generated id types.