Mapping database data types

One of the primary benefits of using the Entity Engine over direct SQL calls to access data is that OFBiz abstracts the details of how data is physically stored, away from the user. When making a request for data through the Entity Engine, the user does not need to know where the data is physically located or how it is formatted. The Entity Engine handles all the details of data mapping for the user in real time.

Since physical data storage formatting varies widely from database vendor to vendor, the Entity Engine must map these variations to consistent data type values for use within the OFBiz framework.

Getting ready

The following steps have to be performed before adding or modifying data type mapping:

  1. Entity engine field types are configured in separate field type definition files, one for each database in use. To determine which field type definition file to use, consult the entityengine.xml file. Each field type file is defined with a field-type-name element XML declaration.
  2. Find the database configuration declaration for the field(s) you wish to change by locating the datasource element with the appropriate name attribute value. For example, if you wish to change the database field type definitions for the database called localderby, find the name attribute that has the value of localderby.
  3. Once the desired data source is identified and the datasource element is located, observe the field-type-name attribute's value. For example, in the default out-of-the-box entityengine.xml file, the field-type-name value for localderby is derby as shown here:
    <datasource name="localderby"
    <!—Note: file contents removed for clarity -->
    field-type-name="derby"
    
  4. Locate the appropriate field-type element declaration given the field-type-name value from step 3. For example, in the following field-type element, we see that the field type definition file for the derby data source is fieldtypederby.xml (Note: the location of fieldtypederby.xml defaults to ~framework/entity/fieldtype):
    <field-type name="derby" loader="fieldfile"
    location="fieldtypederby.xml"/>
    

How to do it...

Adding or modifying a data type mapping may be performed by following these steps:

  1. Open the existing field type definition file for the database as configured in the entityengine.xml file. Use the process described earlier to find the appropriate file corresponding to the database data type needing modification.
  2. To change an existing field type definition, edit the appropriate entry in the field type definition file. For example, to change the definition of the OFBiz data type currency-amount, edit the field-type-def element as shown here:
    <field-type-def type="currency-amount" sql-type="NUMERIC(18,2)"
    java type="java.math.BigDecimal">
    <validate method="isSignedDouble"/>
    </field-type-def>
    
  3. To add a new field type definition, add an element of type field-type-def to the document. Don't forget to close the element with a</field-type-def> tag.
  4. Save and close the file.
  5. If necessary, run ant clean-install to remove any existing data.
  6. If necessary, run ant run-install to reinstall any seed data.
  7. Restart OFBiz.

How it works...

The Entity Engine automatically maps low-level database data types to data types that are consistent across all Entity Engine-managed databases using the mapping definitions found in field type definition files. In this way, OFBiz offloads the tedious and often error-prone task of converting physical database data storage formats for application use from the developer.

In the following example, an OFBiz data type is shown and then the equivalent storage data types per database are given. Using the Entity Engine as the layer between database access and application logic, the OFBiz developer need only reference the data as an OFBiz data type. There is no need to worry about the vagaries of the individual storage mediums in use. If the underlying database changes, applications will continue to work transparently as applications read and write OFBiz data type(s) to the Entity Engine and not the target database.

OFBiz data type: date-time

   
 

sql-type

sql-type-alias

java-type

Oracle

TIMESTAMP

TIMESTAMP(6)

java.sql.Timestamp

PostgreSQL

TIMESTAMPTZ

java.sql.Timestamp

Creating a new entity definition file

Entities are defined in entity definition XML document files. By convention, entity definition files are located in a containing Component's entitydef directory. This may be changed by editing the Component's ofbiz-component.xml file.

How to do it...

To define a new entity definition file, follow these few steps:

  1. Check the ofbiz-component.xml file to make sure there is an entity-resource element that locates an entity definition file. For example:
    <entity-resource type="model" reader-name="main" loader="main"
    location="entitydef/entitymodel.xml"/>
    
  2. Using your favorite operating system or IDE tool, add entity definition files in the directory locations specified in the ofbiz-component.xml file shown earlier. Make sure each file has the appropriate XML version and XSD declarations in the beginning of the file:
    <?xml version="1.0" encoding="UTF-8"?>
    <entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation=
    "http://ofbiz.apache.org/dtds/entitymodel.xsd">
    
  3. Make sure the new entity definition file has a closing XML element tag (</entitymodel>).
  4. Insert as many entity definitions within this file as needed.
  5. Save and close the file.
  6. Restart OFBiz to make any changes to the ofbiz-component.xml file effective.

How it works...

OFBiz is made aware of entity definition files by placing one or more declarations about the files within a Component's ofbiz-component.xml file. There is one such declaration for each entity definition file located within the containing Component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.14.93