Announcement Announcement Module
No announcement yet.
Interceptor saving of attached Objects in ObjectGraph Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Interceptor saving of attached Objects in ObjectGraph

    Version Spring 1.3
    Version Hibernate 3
    (but would change if neccessary)

    Ive been using Spring with Hibernate in a Rich Client Swing Application for a few month and the App is growing quite ok, but as always requirements change regularly and now i have a question regarding the cascading save of attached objects in Hibernate.
    Is there a possibility to interceptor Hibernates internal save - process when saving attached objects?

    For example: Theres a Customer with addresses (to be exact there is a customer holding an object named company that hold the addresses, but for this example the easy version will work). When i call save on the Customer using a subclassed HibernateDAOSupport the addresses are saved automatically.

    I know that it is possible to interceptor this save-method to call a Vaildator that would examine the Object and change it the way i want. My Problem is that there are several Objects that hold these addresses - differing by depth in the ObjectGraph - and i always want to do the same thing with these addresses - i want to validate them regarding existing objects in DB. At the moment i guess the only way to do this is iterating the fields by reflection extracting the AdressObjects one by one, but i think this is bad, slow and dirty.

    Is it possible to write an Interceptor or anything else that automatically triggers on each save of an Object of Type Address without decoupling the Addresses from my ObjectGraph.

    I hope you understand what my problem is. Thank you for reading (and posting). Greetings JoeGranada

  • #2
    Wouldn't the Hibernate interceptor mechanism work for this case? Hibernate reference documentation has an example of using an interceptor.


    • #3
      When dealing with cascading it is best to change the settings directly from the Hibernate mapping files; it's easy, clean and documented.
      You can use, as sabarish said Hibernate interceptors or, if using Hibernate 3.x lifecycle listeners but you'll have to understand the Hibernate API and semantics.
      Cascading, especially when dealing with complex graph, can become tricky if you try to implement it yourself.


      • #4
        This was helpful! Thank you

        It finally works.
        Thank you very much for your help, Costin Leau and sabarish. I use the Hibernate-Events now and they work just great. It took me a while to find the right configuration entries for my springconfig by browsing other threads but i finally made it.

        But there is one thing i do not understand, Costin.
        When dealing with cascading it is best to change the settings directly from the Hibernate mapping files; it's easy, clean and documented.
        What do you mean? Is there a way to tell Hibernate within the mapping-files to look for changes and follow some kind of update-strategy or do you mean the insert and update properties inside the <one-to-many>-relation?


        • #5
          You can instruct Hibernate what events to cascade - for example only save can be cascaded or no event at all.
          Take a look at the hibernate documentation for possible values or at the XML dtd (which in some cases contain information not found in the reference docs).