[ColdBox 3.81] BaseORMservice DeleteByID vs Delete method

Hi All,

We were using the DeletebyID method in

`
ormService.deleteByID( entityName=“someentity”,id=rc.Id,flush=true )

`

In our case, we noticed this had several problems. The deletebyID method could use a list of id’s so you can remove multiple entities. If you are only using ONE entity to delete you could also
first get the object bij ID and then remove the object by the delete method

`
myObject=ormService.get( entityName=“someentity”,rc.id)
ormService.delete(entity=myObject,flush=true);

//or shorter
ormService.delete(entity=ormService.get( entityName=“someentity”,rc.id),flush=true);

`

Our problems:
First:
When you try to delete an object if it has many-to-many properties if will fail on some dependencies. It will not clean up relationships with intermediate tables. The second method will work without problems.

Second:
More important: our system depens on some Orm events. Deletes will be logged and in preDelete we will check if deletion is allowed. The deletebyID method will NOT trigger any preDelete event, so we need other ways to protect against unwanted deletion. By using the second method, events will be fire and our objects are protected against unauthorized deletion.

The deleteAll method shows the same behaviour. I think this is caused by the fact that both methods use HQL for their delete actions, and object used in HQl will not trigger events, at least not for deletions, as we tested.

We could easily work around these limitations, but I thought this was worthwile mentioning (maybe some documentation update). I was not expecting this behaviour.

Wil

HQL should still fire ORM Events, can you confirm if you have the event handler defined in your Application.cfc or are you using it in your entity itself.

Hi,

Make sure you have correct orm-settings in Application.cfc …

example of Application.cfc setting

this.ormSettings = {
// ENTITY LOCATIONS, ADD MORE LOCATIONS AS YOU SEE FIT
cfclocation=[“model”,“modules”],
// THE DIALECT OF YOUR DATABASE OR LET HIBERNATE FIGURE IT OUT, UP TO YOU
dialect = “org.hibernate.dialect.MySQL5Dialect”,
// DO NOT REMOVE THE FOLLOWING LINE OR AUTO-UPDATES MIGHT FAIL.
//dbcreate = “update”,
// FILL OUT: IF YOU WANT CHANGE SECONDARY CACHE, PLEASE UPDATE HERE
//secondarycacheenabled = true,
//cacheprovider = “ehCache”,
// ORM SESSION MANAGEMENT SETTINGS, DO NOT CHANGE
logSQL = true,
flushAtRequestEnd = false,
autoManageSession = false,
// ORM EVENTS MUST BE TURNED ON FOR ECOM-APP TO WORK
eventHandling = true,
eventHandler = “coldbox.system.orm.hibernate.WBEventHandler”,
// THIS IS ADDED SO OTHER CFML ENGINES CAN WORK WITH
skipCFCWithError = false
};

Hi Andrew,

Should fire, yes, I agree. But did you test it yourself? I am quite sure we configured our orm events and ORM interceptors correctly.
I tested this again. I am using an ORM interceptor, and if I am using the ormservice.delete(someobject) the interceptor will fire and the ormservivce.deletebyID will NOT fire.

It is no problem since we know it, but I thought it would be interesting to share with other users. Probably this is more a coldfusion problem and not a coldbox problem, but in the deletebyID method the HQL is quite hidden, so it is good to know.

Wil