Injecting Settings into an Entity Object?

Am I missing something but how do you inject a setting into a
persistent (ORM) entity?

I have my setting in Coldbox.cfc as...

settings.mySetting = "I am a setting";

...then in my entity I have this...

property name="mySetting" type="coldbox:setting:mySetting";

...but CF chokes saying...

Error Type: org.hibernate.MappingException : [N/A]
Error Messages: Could not determine type for:
coldbox:setting:mySetting, at table: tblMyTable, for columns:
[org.hibernate.mapping.Column(mySetting)]

...like it trying to map to a db column.

What I have I misunderstood?

Hi Richard,

The property should be non persistent. So it should be like this
property name="mySetting" type="coldbox:setting:mySetting"
persistent="false";

Thanks

I might be wrong but I believe the type attribute for ColdBox 3 DI has been deprecated in favour of inject so you would have:

property name=“mySetting” inject=“coldbox:setting:mySetting” persistent=“false”;

Sorry chaps, both...

property name="mySetting" type="coldbox:setting:mySetting"
persistent="false";

...and...

property name="mySetting"
inject="coldbox:setting:mySetting" persistent="false";

...didn't work for me :frowning:

What scope should the var be posted into? Variables, right?

Well I didn't see it in there but there was an [empty string] variable
called "mySetting" in the properties of the THIS scope???

In order for this to work you MUST use the ORM Event handler. Remember that Coldbox needs to know about your entities and needs the ORM to talk to ColdBox. So please read the ORM Event Handler guide so you can create the global event handler and activate the entity injector so you can do DI on entities.

Also remember that the DI features are available via the orm event handler via the postLoad() event, so that means that entities created via entityNew() or createObject() will NOT be wired. You will have to wired them manually for now, we are still investigating on our best approach to deal with new entities either from the orm base services we have or via getModel() or getEntity() methods. So pleaes be aware of this as coldfusion does NOT offer a method for listenting when new entities are created.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Sorry Luis, you've left me there.

I have already implemented...

this.ormSettings = eventhandling = "true"
this.ormSettings = eventhandler = "model.ORMEventHandler"

...and have this in my model.ORMEventHandler.cfc...

component extends="coldbox.system.orm.hibernate.EventHandler"{}

I then take it, from what you're saying, that if I use ORMService.new() I
can't make use of postLoad() or any of the other event methods.

So if that is the case how can I access a simple custom setting from within
my entity?

Ciao for now,

Richard

This is a problem with the way ORM works in CF. When you call EntityNew(), then none of the event handlers fire, so you can’t use them for Injection.

What you can do is use a “new” method in your service, which then wires up the object before passing it back. I’ve just looked at the new method in the system.orm.hibernate.BaseORMService and it’s not doing this at the moment. So you’d need to add the autowire, something like:

/**

  • Get a new entity object by entity name and you can pass in any named parameter and the method will try to set it for you.
    */
    any function new(required string entityName){
    var entity = entityNew(arguments.entityName);
    var key = “”;

// iterate over arguments
for( key in arguments ){

// Check if method exists and not entityName
if( key NEQ “entityName” and structKeyExists(entity, “set#key#”) ){
evaluate(“entity.set#key#( arguments[key] )”);
}
}
// do some injection here…
getPlugin(“BeanFactory”).autowire(entity);
// end of DI
return entity;
}

It does mean that you’ll always need to use the service’s new() method to get a new entity and not EntityNew() or the new keyword.

I have no idea if that will work, as when I’ve done it in the past it was using a different framework and Brian Kotek’s beanInjector, but it makes sense in my head :slight_smile:

  • John

Hi Richard,

As John Whish said, EnittyNew() would not call any ORM event handler,
thats why coldbox does not inject the dependencies. I will make some
changes in coldbox.system.orm.hibernate.BaseORMService "new" method
for EntityNew to inject the dependencies.

The work around at the moment is use enityload(name, id which does not
exist), so you will have empty object.

Please let us know if this works for you....?

Thanks
Sana

Richard,

You can fire the postLoad() immediately after you do a
ORMService.new()

Something like this.
var eh = new core.model.ORMEventHandler();
oUser=ORMService.new("User");
eh.postLoad(oUser);

Curt Gratz
Computer Know How
ColdBox Alliance Partner

There are several approaches I am thinking at the moment due to this failure of CF to fire an interceptor on entity creation. one is to treat all entity creations via a service layer that either talks to the base ORM services in coldbox or custom ones. My approach is to the base ORM service and I see two approaches and need you guys input.

  1. Have another property for the service which denotes a flag that all new entity creations via the service layer will be wired via DI in wirebox.

property name=“entityAutowire” and it will default to FALSE.

If this property is set to true, then the NEW() methods or NEW() dynamic methods will create and then do DI on the entities.

  1. Have another special argument in the New() method: autowire = false, that if passed true it will autowire the entity created.

So what approach makes sense to everybody, I have a preference but I would like to see the opinions of you guys first.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Hi Luis, I think I prefer the 2nd approach.

I'm with John, I like the 2nd approach.

Anymore takers?

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Did we pick the wrong option Luis? :smiley:

No, just more feedback? HEHE?

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Ok, I took a different approach.

I created a new interception point whenever new entities are created so it can be more flexible now, so even developers can enhance entities upon creation: ORMPostNew

I then hooked up the autowire interceptor to this new interception point and added a new setting to the interceptor: entityInjection which defaults to false. You can then from your configuration enable entity injection and choose how the autowire should behave.

The only requirements is 1) the ORM event handler is enabled 2) YOu use the base services for entity creation, then you create all this goodness.

What’s great about the interception point is that it will be hooked to wirebox so later on you can even perform AOP on entities via that interception point.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Anyways, retracing my steps, I might re-evaluate some things for consistency as the postLoad() uses the event handlers’ annotations for injections.

So now I am divded on whether it should be controlled by the autowire interceptor or the orm event handler.

I am leaning towards letting the autowire interceptor handle all the injections instead of the orm event handler.

Feedback?

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Hi Luis,

Using the autowire interceptor makes sense to me.

  • John

Luis,

I think autowire interceptor should handle all the injections instead of ORM event handler.
This way coldbox would be more compatible to Railo-ORM.

Not sure if Railo would implement CFIDE…

Thanks
Sana

The only thing is that you need the ORM event handler defined, that is how CF did it and it has to proxy in the requests. So no way of leaving that one. But yes, the autowire interceptor will be the controlling object for entity injections via the ORM evnet handler

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com