I’m using Morphia and MongoDB with my Spring application. I see in many of the example projects that many of the service interface methods throw a DataAccessException. From what I can tell, this exception is thrown from various framework classes to simplify exception handling for various implementations of data access.
At this point I’m guessing I should catch any errors thrown by Morphia and throw a DataAccessException from my service implementation. So my question is, should I model this approach with my service implementations that use Morphia? Or perhaps I’m just misunderstanding this.
This makes sense if you want your business logic to be able to react to specific types of
DataAccessException, without being dependent on the Morphia/Mongo types.The easiest way to do this is to write a class which implements
PersistenceExceptionTranslator, and which knows how to translate the Morphia/Mongo exceptions intoDataAccessException. Declare this class as a bean, and Spring will automatically ask it to translate the exceptions if your DAO class is annotated with@Repository.However, if your business logic or exception-handling logic doesn’t really care which exception type is thrown, then there would seem to be little point.