I have two tables that exist in the same Oracle database system but different schemas, which I’ve mapped like this:
ABC.Store:
component schema="ABC" table="Stores"
{
property name="Id" fieldtype="id" generator="sequence" sequence="store_id_seq";
property name="Products" fieldtype="one-to-many" cfc="Product";
}
DEF.Product:
component schema="DEF" table="Products"
{
property name="Id" fieldtype="id" generator="sequence" sequence="product_id_seq";
}
I set my application’s default datasource as this.datasource = "ABC" in application.cfc.
The problem I’m running into here is whenever I try to save a Product. ColdFusion spits out an error that says the sequence cannot be found for the Id property on Product. This is because the product_id_seq sequence is in the DEF schema, but ColdFusion is trying to find it in the ABC schema, even though I set the schema on the Product as DEF.
If I set the datasource attribute on Product to DEF, I then get an error that says the Products property on Store is unmapped. This is because, as the ColdFusion documentation states:
“Since a Hibernate configuration uses a single data source, all related CFCs (using ORM relationships) must have the same data source.”
My question then is, how do I map the two tables in two different schemas, using a sequence as an ID generator?
I’ve been able to get it to work if I specify the schema for the sequence:
property name="Id" fieldtype="id" generator="sequence" sequence="def.product_id_seq";
But this is hard-coded and I’d like it to be dynamic and pull the schema name from a configuration bean.
The only way I’ve been able to get this to work seamlessly was to:
datasourceattribute in all desired persistent objects to the newly created datasource.schemaattribute in all desired persistent objects to reference the correct schema, or database. (the two are synonymous in ColdFusion ORM)Note: Be sure to use full component path when referencing CFCs in your COM.