I've got this code
Listener Class
****************************************
public class HibernateEventListener implements SaveOrUpdateEventListener {
private static final long serialVersionUID = 8991739587379538184L;
/**
* When a {@link VersionedObject} or a {@link Version} is updated, update
* its old versions so they get their end times set appropriately.
*/
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
Object obj = event.getObject();
System.out.println("in listener" + obj.getClass());
if (obj instanceof VersionedObject<?, ?>) {
((VersionedObject<?, ?>) obj).updateOldVersions(event.getSession());
} else if (obj instanceof Version<?>) {
((Version<?>) obj).getContinuity().updateOldVersions(event.getSession());
}
}
}
********************************************
Configuration
********************************************
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.getEventListeners().setSaveOrUpdateEventListeners(
new SaveOrUpdateEventListener[] {new HibernateEventListener(),
new DefaultSaveOrUpdateEventListener()});
return cfg;
*********************************************
My Test
*********************************************
public class TestUpdateCampaign extends CreateTestData {
public void setUp() throws Exception {
super.setUp();
//System.setProperty("com.adapt.clcm.storage.ignoreBuildProperties","true");
}
public void testUpdateCampaign(){
System.out.println("Test in a session");
Campaign myCampaign = csb.getCampaignById("402887740a8fac16010a8fad34b30007", 0);
myCampaign.setBudget(new Double("1002"));
session.update(myCampaign);
}
}
******************************************
Campaign is my object has getters and setters. We use versioned system that means a Campaign will have s set of CampaignVersions, whenever an update on a Campaign occurs, we create a new row of CampaignVersion and persist it into db. Now the problem is in my test when I call session.update(myCampaign), I never get the Campaign Object in my eventListener.
in my event listener I use this code
Object obj = event.getObject();
System.out.println("in listener" + obj.getClass());
However, I never get an object of Campaign. I got a whole list of Cascaded objects of CampaignVersion objects. It looks like it is not session.update(Campaign) that is triggering the event listener, but something else.
any ideas?
|