org.hibernate.shards
Class CrossShardAssociationException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.hibernate.exception.NestableRuntimeException
org.hibernate.HibernateException
org.hibernate.shards.CrossShardAssociationException
- All Implemented Interfaces:
- Serializable, org.hibernate.exception.Nestable
public class CrossShardAssociationException
- extends org.hibernate.HibernateException
Exception thrown when someone attempts to create a cross-shard association.
Here's an example of a cross-shard association. Let's say we have
AccountManager and Account. There is an owned, one-to-many relationship
between AccountManagers and Accounts, and we are sharding by AccountManager.
That means an AccountManager and all her Accounts live on the same shard.
Now suppose you did the following:
public void reassignLeastProfitableAccount(AccountManager mgr1, AccountManager mgr2) {
Account acct = mgr1.removeLeastProfitableAccount();
acct.setAccountManager(mgr2);
mgr2.addAccount(acct);
}
If the 2 managers happen to live on different shards and you were to then
attempt to save the second manager you would receive a
CrossShardAssociationException because the account lives on a different shard
than the manager with which you're attempting to associate it.
Now you'll notice a few things about this example. First, it doesn't really
respect the constraints of an owned one-to-many relationship. If AccountManagers
truly own Accounts (as opposed to just being associated with them), it doesn't
makes sense to reassign an account because the AccountManager is part of that
object's identity. And If the relationship is an association then you
probably shouldn't be using Hibernate Sharding to manage this relationship
because Accounts are going to pass between AccountManagers, which means
Accounts are going to need to pass between shards, which means you're better
off just letting Hibernate manage the relationship between AccountManagers
and account ids and loading the objects uniquely identified by those ids
on your own.
The other thing you'll notice is that if the two managers happen to live on
the same shard this will work just fine. Yup, it will. We can detect
cross-shard relationships. We can't detect risky code. You just need to
be careful here.
- Author:
- maxr@google.com (Max Ross)
- See Also:
- Serialized Form
Fields inherited from class org.hibernate.exception.NestableRuntimeException |
delegate |
Methods inherited from class org.hibernate.exception.NestableRuntimeException |
getCause, getMessage, getMessage, getMessages, getThrowable, getThrowableCount, getThrowables, indexOfThrowable, indexOfThrowable, printPartialStackTrace, printStackTrace, printStackTrace, printStackTrace |
CrossShardAssociationException
public CrossShardAssociationException(Throwable root)
CrossShardAssociationException
public CrossShardAssociationException(String string,
Throwable root)
CrossShardAssociationException
public CrossShardAssociationException(String s)