28
Feb

Setting Up A Persistent Entity In Coldfusion ORM

After we have create our basic ORM settings and enabled ORM in our Application.cfc,  we now need develop the core entities that will serve as the backbone of the application. This is the part where we map our persistent entities to the database tables and columns.  In its most basic form, creating persistent entities is really easy. However, the optional settings can get a little tricky and cause some headaches if they are done correctly.

 

In this example, a persistent entity named person has been created. This component will map a “person” to the database. It will create 4 columns named “PERSON_ID, FIRST_NAME, LAST_NAME, and EMAIL. As long as your ormSettings.dbCreate is set to dropCreate/Update then the tables and columns will be created automatically.

This is all it takes to create a persistent entity. Coldfusion will automatically create getters and setters for each of these properties. As you can imagine this is much easier than creating getFirstName, setFirstName, deleteFirstName functions for all your properties.

Lets go through each of these.

Line 1: We create the component and tell the application that this is a persistent entity. That is to say that we want the properties to be mapped to the database.  You can optionally provide a table name here. I like to do this just so that there is no doubt about what database table is mapped to what component.

Line 3: Here we start creating properties. These properties will map to columns in your database. The first one will be our primary key.  The primary key is denoted by the fieldtype of ID. We also have set the generator type to “native”.  Setting the generator to “native” will use the databases native auto incremental primary key strategy. For example, in MySQL it will set the primary key to auto increment an integer.

Line 4-6: Here we have some basic properties that will map to a column in your database. The name attribute will of course map to a database column. The ormtype will tell the database and hibernate what data type it should be expecting. You can set the ormtype to any of the following common types: string,  text, integer, float, double, Boolean, timestamp, date, binary, blob, and clob. The length attribute will set the maximum length that can be stored in the property.

Tags: ORM; Coldfusion