Announcement Announcement Module
No announcement yet.
Accessing the target object of an ProxyFactoryBean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.