2014-04-01 41 views
0

我試圖在我的應用程序的服務層中處理Spring DAO異常(http://docs.spring.io/spring/docs/4.0.3.RELEASE/spring-framework-reference/htmlsingle/#dao-exceptions),只是爲了發現spring-data-commons模塊中的異常不會擴展org.springframework.dao.DataAccessException中的。 DataAccessException爲什麼spring-data-commons中的異常不會擴展DataAccessException?

例如:PropertyReferenceException

據我所知,此模塊中的所有異常,以及可能在Spring Data項目的其他子模塊中應該延伸DataAccessException

有沒有什麼明顯的,我沒有看到這裏?

回答

0

Spring Data不需要使用Spring Core的DataAccessException層次結構,因爲可以使用PersistenceExceptionTranslator。後者翻譯拋出的異常例如Spring Data Repositories爲DataAccessException子類型。

當使用「@Repository」註釋標記存儲庫時,PersistenceExceptionTranslator自動啓動。如果需要,該服務(使用註釋存儲庫)可能會捕獲DataAccessException。

+1

必須註冊[PersistenceExceptionTranslationPostProcessor](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html#orm-exception-translation)才能激活'@ Repository'註釋接口的異常轉換。 – timomeinen

相關問題