Results 1 to 4 of 4

Thread: Returning Proxy with Hibernate DAO query

  1. #1
    Join Date
    Oct 2004
    Posts
    4

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    109

    Default

    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)
    Thanks,
    Alex.

  3. #3
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  4. #4
    Join Date
    Oct 2004
    Posts
    4

    Default

    Thanks a lot ! That works fine

Similar Threads

  1. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  4. Other Hibernate DAO LazyInitializationExceptions
    By bernardsirius in forum Data
    Replies: 5
    Last Post: Feb 18th, 2005, 04:09 PM
  5. Replies: 8
    Last Post: Sep 23rd, 2004, 01:12 AM

Posting Permissions

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