[ColdBox 3.8.0] Preinsert not firing on virtual entity service

I’m trying to call preinsert on a service that extends an object. The preinsert on virtual entity service is not firing. I’m on railo 4.1. I saw a lot of orm related bug on railo, so I consider this is another one. Could you confirm? Should I switch to good old query?

My Object:

`

component persistent=“true” table=“category” {

property name=“categoryId” generator=“identity” fieldtype=“id”;
property name=“parent” column=“parentId” fieldtype=“many-to-one” CFC=“category” fkcolumn=“parentId”;
property name=“lft” column=“leftId” type=“numeric”;
property name=“rgt” column=“rightId” type=“numeric”;
property name=“name” ormtype=“string” length=“255”;
property name=“description” ormtype=“text”;

// Constructor
public Category function init(){
return this;
}

public function preInsert(){

}
}

`

My service:

`

component extends=“coldbox.system.orm.hibernate.VirtualEntityService” singleton{

/**

  • Constructor
    */
    public CategoryService function init(){

// init super class
super.init(entityName=“Category”);

return this;
}

public void function preInsert() {
super.preInsert();
writeDump(var=“FIRING”,label="");abort;

}

}

`

preInsert() will fire on the entity itself. That entity contains an empty method which does nothing.

So I cannot use preinsert on virtual entity service?

You cannot have an preInsert() method on your entity AND use preInsert() anywhere else.

If I remove preInsert from the entity, the preinsert on service will be fired?

Did you try it?

I have tried, not working!

Inject your service into your entity and use preInsert() inside the entity to call the service…

http://wiki.coldbox.org/wiki/ORM:ORMEventHandler.cfm

These docs should point you in the right direction.

Curt

@Curt I’ve read the documentation. I’ve updated the code:

`

component persistent=“true” table=“category” {

property name=“ormService” inject=“model:categoryService@panel”;

property name=“categoryId” generator=“identity” fieldtype=“id”;
property name=“parent” column=“parentId” fieldtype=“many-to-one” CFC=“category” fkcolumn=“parentId”;
property name=“lft” column=“leftId” type=“numeric”;
property name=“rgt” column=“rightId” type=“numeric”;
property name=“name” ormtype=“string” length=“255”;
property name=“description” ormtype=“text”;

// Constructor
public Category function init(){
return this;
}

public void function preInsert() {
writeDump(var="#ormService#",label="");abort;
}

}

`

I dump the ormservice just to see if it’s injected and to see if preinsert is fired. I got error that ormservice variable does not exists.

Try

property name=“ormService” inject=“categoryService@panel”;

The documents are pretty clear on the problems you’re having.

http://wiki.coldbox.org/wiki/WireBox-EntityInjection/wiki/Dashboard.cfm

I’ve read the documentation. However what is not clear to me is this: if virtual entity service inherits baseorm, why if I create a service that extends my Category.cfc this service does not fire preinsert?

To be more clear: I have a category.cfc object, a CategoryService that extends category.cfc. In categoryService I would like to create a preInsert method to make some operations before I insert the object in database.

Why if I use preinsert on my service this will be not fired?

Hi,

preInsert() should be part of object which will be saved. Like in your example you have category.cfc, if this object have method preInsert() then it will be executed.

I would suggest to check 2 things
1: Application.cfc settings
http://wiki.coldbox.org/wiki/Extras:ORMEventHandler.cfm

2: Video is also part of documentation, which will help in understanding how hibernate events works.

Because even though your service extends ORM Entity, ORM Entity is not loaded as the service. ColdFusion will only load ORM Entities when it needs the data, not what extends it.

Thanks all. It’s more clear now!