Results 1 to 2 of 2

Thread: Accessing the target object of an ProxyFactoryBean

  1. #1
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default Accessing the target object of an ProxyFactoryBean

    This topic has a little mix of different subjects, some Data Access and some AOP with a touch of Transaction.

    I am using Hibernate for Data Access, and a HibernateTemplate. The HibernateTemplate is accessed through a EntityRepository interface implemented by HibernateEntityRepository.

    The interface and the implementation is part of a different project and I do not have oppurtunity to change it.

    The implementation HibernateEntityRepository does ha a method to perform delete, but the interface does not.

    In my code I am accessing through the interface, not the implementation.

    This presents a problem when I want to perform a delete. I would need to cast EntityRepository to an HibernateEntityRepository, but I can't. That is because the EntityRepository is configured as a ProxyFactoryBean to allow for Transaction.

    When I try to cast it, it gives a ClassCastException: $Proxy1.

    Anyone know a solution to this problem?

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Scoped proxies implement an additional interface ScopedObject which provides the method getTargetObject(). For regular proxies a similar mechanism seems to exist: Cast the proxy to Advised, call getTargetSource() and getTarget() on the TargetSource. I have never used this though, so you have to try it out yourself.

    Another alternative in your case seems to switch to class-based proxying using CGLib. It has some downsides or additional requirements though.

    Joerg
    This post can contain insufficient information.

Posting Permissions

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