Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3
What is the best way to have Hibernate call the save methods of my DAOs for objects that are persisted using cascading saves-updates instead of just persisting them for me? I’d like to customize the save behavior for certain objects.
For instance, lets assume I have the following objects:
Speaker
+ String name
+ String specialty
SpeakerDAO
+ Speaker[] list() // list all speakers in DB
+ void save(Speaker obj) // save a particular instance, possibly do some CUSTOM stuff here
Event
+ String title
+ Speaker[] speakers
EventDAO
+ Event[] list() // list all events in DB
+ void save(Event obj) // save a particular instance, possibly do some CUSTOM stuff here
Now lets assume I have defined a hibernate mapping for both Speaker and Event, and that I have setup a many-to-one association in the Event mapping for the “speakers” array; and cascading saves/updates are enabled for the association.
Now in my application, when I persist an Event, I call the “save(Event obj)” method of an EventDAO instance, which ONLY calls the Hibernate session.save(object o) method, passing in the Event object. At this time the Speakers will ALSO be persisted, however, what if I had something custom in the SpeakerDAO.save(Speaker obj) method that should be called when speakers are saved?
How do I get Hibernate to execute my custom SpeakerDAO.save(Speaker obj) method on Speaker objects that are persisted via a cascading save-update instead of just directly persisting them?
This is a bad example because it’s obvious you can just update EventDAO.save to manually pass each Speaker object to the SpeakerDAO.save(Speaker obj) method; but what if I had multiple objects that persisted collections of Speakers? I’d rather not have to remember to manually save each Speaker object using the SpeakerDAO.save(Speaker obj) method specially. I’d rather have hibernate just know that when it’s saving a Speaker object, that it needs to save the Speaker object by calling the save method of the SpeakerDAO class.
Is there any way to do this in Hibernate cleanly? I saw there was an Interceptor Interface. Is the interceptor interface made for such a case?
I’d like to know what’s the best way to go about telling Hibernate that some classes (i.e. Speaker) need to be persisted using a particular class and/or method (i.e. SpeakerDAO.save(Speaker obj)), and not just automatically persisted.
Thank You in advance for your help.