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)]
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.
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
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.
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.
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.
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.
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.
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