Results 1 to 6 of 6

Thread: DataAccessException and implementation of EntityManager

  1. #1

    Default DataAccessException and implementation of EntityManager

    Hello,

    I am not as expert as a lot of other people in here, so please treat me gently

    I have a Spring MVC project where the DAOs and their implementations were done by extending JpaDaoSupport class. For development reasons, Spring has deprecated JpaDaoSupport since the release of 3.1.x. Suggested alternative was EntityManager (which was actually wrapped and used inside JpaDaoSupport). I am using that and managed to pass the new design through my JUnit test harness.

    There is a small issue that is bugging me. Some of my tests were expecting InvalidDataAccessApiUsageException (subclass of DataAccessException in SpringFramework). However, because I have changed the way I implement by DAO (i.e. via EntityManager, not JpaDaoSupport), my tests are failing because it gets IllegalArgumentException instead of InvalidDataAccessApiUsageException.

    I know that when you use @Repository in your DAO implementation, it needs the Exceptions to be translated via PersistenceExceptionTranslationPostProcessor bean (declared in context file). But it is not MENDATORY to annotate your DAO implementation with @Repository as it will do the job (due to @PersistenceContext with EntityManager). I really don't know how to handle this issue.

    Thanks in advance

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Exception translation is in no way tied/bound to the @Repository annotation, although it does make things a lot easier as you don't have to configure anything yourself.

    You can use Spring AOP to configure the org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor to translate the exceptions for your daos.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Quote Originally Posted by Marten Deinum View Post
    Exception translation is in no way tied/bound to the @Repository annotation, although it does make things a lot easier as you don't have to configure anything yourself.
    Marten...do you have any example or suggestion whee I can find more about what you said? I have looked into Spring's documentation and also here - http://blog.springsource.org/2006/08...encing-spring/

    There is no explicit indication of what you said...however, thanks for the heads up and I will keep googling until I find an explanation for myself :-)

    Quote Originally Posted by Marten Deinum View Post
    You can use Spring AOP to configure the org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor to translate the exceptions for your daos.
    Also do you have any example or reference that happen to be at your disposal that I can look into? As I said, I am using Spring,Hibernate,Derby, and Maven just for 3 months and THERE IS AN AWFUL LOT TO LEARN AND MASTER..

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    The PersistenceExceptionTranslatorPostProcessor automatically adds the PersistenceExceptionTranslationInterceptor to beans annotated with @Repository. The interceptor is the thing that takes care of the translation, and an interceptor is plain AOP. (You have to know a little of the internals for spring for this ).

    You can also add the exception translation yourself by specifing a pointcut and the interceptor yourself.

    Code:
    <bean id="exceptionTranslationInterceptor" class="PersistenceExceptionTranslationInterceptor" />
    
    <aop:config>
      <aop:pointcut id="repositories" expression="execution(* com.your.package.*.(..))" />
      <aop:advisor pointcut-ref="repositories" advice-ref="exceptionTranslationInterceptor" />
    </aop:config>
    Something like that...

    The Spring reference guide is a must read and I suggest Spring in Action and/or Pro Spring 3 for Spring.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Smile

    Quote Originally Posted by Marten Deinum View Post
    The PersistenceExceptionTranslatorPostProcessor automatically adds the PersistenceExceptionTranslationInterceptor to beans annotated with @Repository. The interceptor is the thing that takes care of the translation, and an interceptor is plain AOP. (You have to know a little of the internals for spring for this ).

    You can also add the exception translation yourself by specifing a pointcut and the interceptor yourself.

    Code:
    <bean id="exceptionTranslationInterceptor" class="PersistenceExceptionTranslationInterceptor" />
    
    <aop:config>
      <aop:pointcut id="repositories" expression="execution(* com.your.package.*.(..))" />
      <aop:advisor pointcut-ref="repositories" advice-ref="exceptionTranslationInterceptor" />
    </aop:config>
    Something like that...

    The Spring reference guide is a must read and I suggest Spring in Action and/or Pro Spring 3 for Spring.
    Marten,

    Thanks a lot...it helps to unwind my confusion noodles Just one last question if you don't mind....I know that I will have to find this out by myself, but do you think that the javax.persistence.EntityManager Interface and its implementations throw java.lang.RuntimeException (e.g. NullPointerException or IllegalArgumentException) rather than any extended ones (e.g. Spring NonTransientDataAccessException) ?

    The reason I am asking is because (as I posted in the original message) when I was extending JpaDaoSupport to implement my DAO, some of my tests were expected InvalidDataAccessApiUsageException (due to saving or updating a NULL item)and were getting it too. But because I am now using the EntityManager in my DAO implemenation, all those tests where I was getting InvalidDataAccessApiUsageException are now throwing IllegalArgumentException. Does it mean that JpaDaoSupport was implicitly taking care of the exception translation?

    Sincere apologies if I have asked anything quite obvious

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I'm pretty sure the EntityManager will not throw any Spring exceptions . For the rest it depends also the thrown exceptions are pretty well documented on the EntityManager (JPA spec) api.

    JpaTemplate not the JpaDaoSupport also did exception translation it might do a bit more then the other translation it might depend upon your provider.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •