Announcement Announcement Module
No announcement yet.
Returning Proxy with Hibernate DAO query Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning Proxy with Hibernate DAO query

    Is it possible to implement a DAO (with Hibernate queries) to return Proxy classes ?

    Example :
    public List getCompanyByName(final String name) {
    Session session = SessionFactoryUtils.getSession(getSessionFactory() , false);
    try {
    return session.find("from Company company where", name, Hibernate.STRING);
    } catch (HibernateException e) {
    return null;

    This method return Company object and not the Proxy of the Company.

    <bean id="companyImpl" class="CompanyImpl">

    <bean id="company" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces">
    <property name="target">
    <ref local="companyImpl"/>

    <property name="interceptorNames">


  • #2
    Hibernate creates the returned object spring is just a middle-man (or a woman whichever one you like) that tries to eliminate the oddities of all the plumbing code that you would have to otherwise write yourself; and of course all the transactional stuff and bunch of other things that are not relevant for this particular discussion. So to create proxies you would have to ask hibernate to do it and as AFAIK it is not currently possible (and I am not even sure if it is in hibernate's roadmap). Note that hibenrate will create a proxy for you but only for lazy instantiation not for you to mingle with it :-(

    There were a few discussions about topics similar to this one. Search thru the discussions (especially about hibernate managed objects)


    • #3
      You could write a Spring (or rather, AOP Alliance) MethodInterceptor around your DAO that replaced the return value from Hibernate with a proxy or collection of proxies. You would wrap them programmatically using Spring's ProxyFactory. I think you could then modify those objects successfully, as the target would still be tracked by Hibernate and would be transparently persisted. I have done something similar for an unusual dirty-tracking requirement where I wanted to monitor changes to an object graph.


      • #4
        Thanks a lot ! That works fine