Announcement Announcement Module
Collapse
No announcement yet.
Returning Proxy with Hibernate DAO query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 company.name=?", name, Hibernate.STRING);
    } catch (HibernateException e) {
    e.printStackTrace();
    }
    return null;
    }

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

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

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

    <property name="interceptorNames">
    <list>
    <value>debugInterceptor</value>
    </list>
    </property>
    </bean>

    Thanks...

  • #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)

    Comment


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

      Comment


      • #4
        Thanks a lot ! That works fine

        Comment

        Working...
        X