Monday, September 17, 2007

Hibernate Shards

From: http://www.hibernate.org/hib_docs/shards/reference/en/html/shards-shardstrategy.html

"It's important to note that if a multi-level object graph is being saved via Hibernate's cascading functionality, the ShardSelectionStrategy will only be consulted when saving the top-level object. All child objects will automatically be saved to the same shard as the parent. You may find your ShardSelectionStrategy easier to implement if you prevent developers from creating new objects at more than one level in your object hierarchy. You can accomplish this by making your ShardSelectionStrategy implementation aware of the top-level objects in your model and having it throw an exception if it encounters an object that is not in this set. If you do not wish to impose this restriction that's fine, just remember that if you're doing attribute-based shard selection, the attributes you use to make your decision need to be available on every object that gets passed to session.save()."

If you were to save child objects in a different shard, will you be able to enforce foreign key constraint? More generally, is it possible to enforce cross-shard integrity reference? Answer below (http://www.hibernate.org/hib_docs/shards/reference/en/html/shards-resharding.html):

"Unfortunately, the problem of changing object-shard mappings becomes even worse once we consider the fact that Hibernate Shards does not support cross-shard relationships (meaning FKs). This limitation prevents us from moving a subset of an object graph from one shard to another."

1 comment:

Max said...

Hi Cheng,

Just wanted to point out that there can be no such thing as an eagerly fetched cross-shard relationship - it will always require a separate trip to the database to resolve.
This makes it a bit dangerous to think of a cross-shard relationship the same way you might think about an intra-shard relationship, where you choose a default fetch policy (typically lazy), and then override that fetch policy to be eager for the specific cases where you need the performance boost of pulling back the associated objects as part of the same query. This optimization isn't available with cross-shard relationships. Of course, cross-shard relationships aren't available either. Yet. :-)

Max (lead Shards developer)