Status/Resolution/Reason: Needs Review//
Reporter/Name(from Bugbase): Alexandre Potvin Latreille / Alexandre Potvin Latreille (Alexandre Potvin Latreille)
Created: 06/27/2015
Components: ORM Support
Versions: 11.0
Failure Type: Enhancement Request
Found In Build/Fixed In Build: CF11_Final /
Priority/Frequency: Trivial / Unknown
Locale/System: English / Win 2008 Server x64
Vote Count: 0
Consider the following example where an OverbookingPolicy value object is associated to a Course in order to specify by how much % a course might be overbooked. We me have a DefaultOverbookingPolicy and a NoOverbookingPolicy for instance (it's a fictional example).
//Course.cfc
component {
property name="id";
property name="overbookingPolicy";
property name="maximumCapacity";
property name="registrations";
public function init(required guid id, required OverbookingPolicy overbookingPolicy, required numeric maximumCapacity) {
variables.id = arguments.id;
variables.overbookingPolicy = arguments.overbookingPolicy;
variables.maximumCapacity = arguments.maximumCapacity;
variables.registrations = [];
}
public function register(Student student) {
if (overbookingPolicy.willBeExceeded(arrayLen(registrations), maximumCapacity)) throw ...;
arrayAppend(registrations, new CourseRegistration(id, arguments.student.id()));
}
}
With Hibernate, the mapping for OverbookingPolicy could be handled using a custom UserType. If we had an OverbookingPolicyFactory which can create policies from a string type, we could just store the type in the database and use the custom UserType to call on the factory to return the correct instance when hibernate is reconstructing the object.
I haven't found a way to implement Hibernate custom UserTypes in ColdFusion?
Workarounds:
1. OverbookingPolicy cannot be an interface, it must be a concrete class which will be used as an abstract class.
2. OverbookingPolicy will have a type string property.
3. Use a <component> mapping:
<component name="overbookingPolicy" class="cfc:OverbookingPolicy">
<property name="type" type="string" column="overbooking_policy_type" />
</component>
4. Once Hibernate will re-hydrate the overbookingPolicy property on Course, it will be an instance of the OverbookingPolicy base class, but with the appropriate type.
5. Create a custom getOverbookingPolicy() function on Course and always use that function to access the overbooking policy.
public OverbookingPolicy getOverbookingPolicy() {
//overbookingPolicy might not be of the correct concrete type, so to make sure we
//ask the instance from the factory everytime.
return new OverbookingPolicyFactory.fromType(overbookingPolicy.getType());
}
There might be other ways of working around that mapping problem. For instance, I imagine that if your types are stored in the database, then you could map the overbookingPolicy property as en entity rather than a component, but having support for custom Hibernate UserTypes and CompositeUserTypes would be great.
----------------------------- Additional Watson Details -----------------------------
Watson Bug ID: 4013664
External Customer Info:
External Company:
External Customer Name: Alexandre
External Customer Email:
External Test Config: My Hardware and Environment details:
Attachments:
Comments: