One of the most convenient thing I love about ColdFusion ORM is that it lets you build your database automatically from the object model. It generates the table from the CFC mapping that you have provided, automatically defines all the necessary constraints from the relationships or property definitions and allows you to populate the database with some initial data using a script file. It also allows you to keep your database always in sync with your application, which means that if you add a new CFC in your application or if you add new properties in your components, you don’t have to go to the database and add them. CF-ORM (or Hibernate) will do that automatically for you.

Though this is really nice, ColdFusion does not do it by default. You need to enable it specifically if you want it to build the database for you. You can do this by setting ‘ormsettings.dbCreate‘ to ‘dropcreate‘ or ‘update‘ in application.

ormsettings.dbcreate can have following values

dropcreate : With this setting, CF-ORM drops the table if it exists and then creates it. This starts the application with a clean slate and one should be careful while using this. Careful because all your data will be deleted and tables will be created afresh whenever the application starts or when ever ORM is initialized. This setting is awesome at the development time. With this setting, you can also specify a sqlscript file to initially populate the tables once they are created. You can specify that using ormsettings.sqlscript in application.

update : With this setting, CF-ORM will create the table if it does not exist or update it if it exists. This setting is very convenient as you don’t need to make any changes in the database table yourself whenever changes are made in the application. Hibernate will do that for you. However that is not absolutely true all the time. (I can see lot of people complaining/logging bugs about it :-) ). Here is what Hibernate does when you have this setting on :

  • Create table if a new CFC mapping is found in the application or if the table name of a CFC is changed. If the table name is changed for a CFC, it will not rename the table in the DB. It will simply create a new table leaving the old table as it is.
  • For existing table, add column if a new property is added or if the column name for property is changed. If the column name is changed for a property, it will not rename the column but will simply create another column with the new name.
  • For existing column, add the constraints, if a new foreign key constraint is required for relationship. However none of these are modified in the table -  datatype, length, not-null, unique, precision, scale, index, uniqueKey.
  • Change the id generation strategy if generator is changed for id column.

none : This is the default setting where tables are not created or modified by CF-ORM. It uses the existing tables in the database. One should switch to this setting once the application goes in production.