Apache OFBiz Entity Engine

Exclusive offer: get 50% off this eBook here
Apache OfBiz Cookbook

Apache OfBiz Cookbook — Save 50%

Over 60 simple but incredibly effective recipes for taking control of OFBiz

$26.99    $13.50
by Ruth Hoffman | November 2010 | Cookbooks Open Source

This article, by Ruth Hoffman, author of Apache OFBiz, discuss' the recipe's for OFBiz entity engine.

This article will cover:

  • Changing the default database
  • Connecting to a remote database
  • Connecting to multiple databases
  • Creating entity groups
  • Disabling automatic database checks
  • Mapping database data types
  • Creating a new entity model
  • Modifying an existing entity model
  • Building a view-entity

 

Apache OfBiz Cookbook

Apache OfBiz Cookbook

Over 60 simple but incredibly effective recipes for taking control of OFBiz

  • Optimize your OFBiz experience and save hours of frustration with this timesaving collection of practical recipes covering a wide range of OFBiz topics.
  • Get answers to the most commonly asked OFBiz questions in an easy-to-digest reference style of presentation.
  • Discover insights into OFBiz design, implementation, and best practices by exploring real-life solutions.
  • Each recipe in this Cookbook is crafted to describe not only "how" to accomplish a specific task, but also "why" the technique works to ensure you get the most out of your OFBiz implementation.

        Read more about this book      

(For more resources on Apache, see here.)

Introduction

Secure and reliable data storage is the key business driver behind any data management strategy. That OFBiz takes data management seriously and does not leave all the tedious and error-prone data management tasks to the application developer or the integrator is evident from the visionary design and implementation of the Entity Engine.

The Entity Engine is a database agnostic application development and deployment framework seamlessly integrated into the OFBiz project code. It handles all the day-to-day data management tasks necessary to securely and reliably operate an enterprise. These tasks include, but are not limited to support for:

  • Simultaneously connecting to an unlimited number of databases
  • Managing an unlimited number of database connection pools
  • Overseeing database transactions
  • Handling database error conditions

The true power of the Entity Engine is that it provides OFBiz Applications with all the tools, utilities, and an Application Programming Interface (API) necessary to easily read and write data to all configured data sources in a consistent and predictable manner without concern for database connections, the physical location of the data, or the underlying data type.

To best understand how to effectively use the Entity Engine to meet all your data storage needs, a quick review of Relational Database Management Systems (RDBMS) is in order:

  • RDBMS tables are the basic organizational structure of a relational database. An OFBiz entity is a model of a database table. As a model, entities describe a table's structure, content format, and any applicable associations a table may have with other tables.
  • Database tables are further broken down into one or more columns. Table columns have data type and format characteristics constrained by the underlying RDBMS and assigned to them as part of a table's definition. The entity model describes a mapping of table columns to entity fields.
  • Physically, data is stored in tables as one or more rows. A record is a unique instance of the content within a table's row. Users access table records by reading and writing one or more rows as mapped by an entity's model. In OFBiz, records are called entity values.
  • Keys are a special type of field. Although there are several types of keys, OFBiz is primarily concerned with primary keys and foreign keys. A table's primary key is a column or group of columns that uniquely identifies a row within a table. The value of the primary key uniquely identifies a table's row throughout the entire database.
  • A foreign key is a key used in one table to represent the value of a primary key in a related table. Foreign keys are used to establish unique and referentially correct relationships between one or more tables.
  • Relationships are any associations that tables may have with one another.
  • Views are "virtual" tables composed of columns from one or more tables in the database. OFBiz has a similar construct (although it differs from the traditional RDBMS definition of a "view") in the view-entity.

Note: while this discussion has focused on RDMS, there is nothing to preclude you from using the Entity Engine in conjunction with any other types of data source(s).

The Entity Engine provides all the tools and utilities necessary to effectively and securely access an unlimited number of databases regardless of the physical location of the data source, as shown in the following figure:

Changing the default database

Out-of-the-box, OFBiz is integrated with the Apache Derby database system (http://db.apache.org/derby). While Derby is sufficient to handle OFBiz during software development, evaluation, and functional testing, it is not recommended for environments that experience high transaction volumes. In particular, it is not recommended for use in production environments.

Getting ready

Before configuring an external database, the following few steps have to be ensured:

  1. Before changing the OFBiz Entity Engine configuration to use a remote data source, you must first create the remote database; the remote database must exist.

Note: if you are not going to install the OFBiz schema and/or seed data on the remote database, but rather intend to use it as is, you will not need to create a database. You will need, however, to define entities for each remote database table you wish to access, and assign those entities to one or more entity groups.

  1. Add a user/owner for the remote database. OFBiz will access the database as this user. Make sure the user has all necessary privileges to create and remove database tables.
  2. Add a user/owner password (if desired or necessary) to the remote database.
  3. Ensure that the IP port the database is listening on for remote connections is open and clear of any firewall obstructions (for example, by default, PostgreSQL listens for connections on port 5432).
  4. Add the appropriate database driver to the ~framework/entity/lib/jdbc directory.

For example, if you are using PostgreSQL version 8.3, download the postgresql-8.3-605.jdbc2.jar driver from the PostgreSQL website (http://jdbc.postgresql.org/download.html).

How to do it...

To configure another external database, follow these few steps:

  1. Open the Entity Engine's configuration file located at:

    ~framework/entity/config/entityengine.xml

  2. Within the entityengine.xml file, configure the remote database's usage settings. A suggested method for doing this is to take an existing datasource element entry and modify that to reflect the necessary settings for a remote database. There are examples provided for most of the commonly used databases.

    For example, to configure a remote PostgreSQL database with the name of myofbiz_db, with a username ofbiz and password of ofbiz, edit the localpostnew configuration entry as shown here:

    <datasource name="localpostnew"
    helper-class=
    "org.ofbiz.entity.datasource.GenericHelperDAO"
    schema-name="public"
    field-type-name="postnew"
    check-on-start="true"
    add-missing-on-start="true"
    use-fk-initially-deferred="false"
    alias-view-columns="false"
    join-style="ansi"
    result-fetch-size="50"
    use-binary-type-for-blob="true">
    <read-data reader-name="seed"/>
    <read-data reader-name="seed-initial"/>
    <read-data reader-name="demo"/>
    <read-data reader-name="ext"/>
    <inline-jdbc jdbc-driver="org.postgresql.Driver"
    jdbc-uri="jdbc:postgresql://127.0.0.1/myofbiz_db"
    jdbc-username="ofbiz"
    jdbc-password="ofbiz"
    isolation-level="ReadCommitted"
    pool-minsize="2"
    pool-maxsize="250"/>
    </datasource>

  3. Configure the default delegator for this data source:

    <delegator name="default" entity-model-reader="main"
    entity-group-reader="main" entity-eca-reader="main"
    distributed-cache-clear-enabled="false">
    <group-map group-name="org.ofbiz"
    datasource-name="localpostnew"/>
    <group-map group-name="org.ofbiz.olap"
    datasource-name="localderbyolap"/>
    </delegator>

  4. Save and close the entityengine.xml file.
  5. From the OFBiz install directory, rebuild OFBiz by running the ant run-install command.
  6. Start OFBiz.
  7. Test by observing that the database was created and populated. You may use the WebTools entity reference page (https://localhost:8443/webtools/control/entityref) to search for your newly created entities, or a third-party tool designed to work with your specific database.

How it works...

The Entity Engine is configured using the entityengine.xml file. Whenever OFBiz is restarted, the Entity Engine initializes itself by first referencing this file, and then building and testing all the designated database connections. In this way, an unlimited number of data source connections, database types, and even low-level driver combinations may be applied at runtime without affecting the higher-level database access logic.

By abstracting the connection using one or more delegators, OFBiz further offloads lowlevel database connection management from the developer, and handles all connection maintenance, data mappings, and the default transaction configuration for an unlimited number of target databases.

To configure one or more database connections, add datasource element declarations with settings as shown here:

To specify that the Entity Engine should be connected to a database using a JDBC driver and to configure the specific connection parameters to pass, set the inline-jdbc element attributes as detailed here:

Connecting to a remote database

A "remote" database is any data source that is not the default Derby database. A remote database may be network connected and/or installed on the local server. The Entity Engine supports simultaneous connections to an unlimited number of remote databases in addition to, or as a replacement for, the default instance of Derby.

Each remote database connection requires a datasource element entry in the entityengine.xml file. Adding and removing database connections may be performed at any time; however, entityengine.xml file changes are only effective upon OFBiz restart.

Apache OfBiz Cookbook Over 60 simple but incredibly effective recipes for taking control of OFBiz
Published: September 2010
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

        Read more about this book      

(For more resources on Apache, see here.)

Getting ready

Collect all the necessary information required to connect to the remote database. In particular, you will need, at a minimum, the following information:

  • Database name.
  • Username and password for OFBiz to pass when negotiating connections to the remote database. In some cases, passwords are not required. Check your remote database setup for more information.
  • Database schema name, if appropriate. See the entityengine.xml file for examples.
  • Connection location information, including the IP address and port number to connect on (for IP-based network connections).

How to do it...

Once the initial information is collected, a remote database can be connected to by following these steps:

  1. Edit the entityengine.xml file. Add one or more datasource elements, one for each target database. Add one or more delegator elements with entity group configuration information telling the Entity Engine which entities are to be found on which target databases.
  2. Add or modify any field type definitions in the appropriate field type definition file for the target remote database.
  3. Add all necessary entity definitions to one or more entity definition file(s). Each table on the remote database requires an entity definition before it may be accessed through the Entity Engine.
  4. If necessary, add any entity group definitions to an entity group definition file.
  5. Make sure that any new entity group definition file is defined in the ofbiz-component.xml file.
  6. To make any Entity Engine configuration changes effective, restart OFBiz.

How it works...

Entity engine database connections and entity models are built whenever OFBiz is restarted as part of its initialization process. To manage database connections, connection pooling across all defined databases, data transfer, and data source transaction specifics, OFBiz consults the entityengine.xml file and automatically builds the internal control settings each time it is restarted.

Connecting to multiple databases

The OFBiz Entity Engine may service an unlimited number of database connections in addition to, or as a replacement for, the default embedded Derby database.

Getting ready

The following steps should be ensured:

  1. If the databases do not already exist, be sure to create them prior to editing the entityengine.xml file. Collect necessary connection information per database, including database location information (IP address and port), database name, username, and password if necessary.
  2. Create one or more entity group definition(s) that assign a connection label using the delegator declaration to an Entity Engine reader. If you wish to create a new entity group definition file for the new entity model's group definitions, do so now. (Note: you can use an existing entity group definition file such as the one found in ~framework/entity/entitydef/entitygroup.xml).
  3. If the entity model definitions do not exist for the target database tables, create one entity definition for each target database table and assign that entity(s) to the appropriate entity group.

How to do it...

To add one or more databases to the Entity Engine configuration, edit the ~framework/entity/config/entityengine.xml file as shown here:

  1. Open the entityengine.xml file using a text editor of your choice.
  2. Within the entityengine.xml file, configure each database as a separate datasource element.
  3. If desired, define a unique delegator element and group-map.
  4. Save and close the entityengine.xml file.
  5. Restart OFBiz.

How it works...

The Entity Engine rebuilds its runtime configuration of configured data sources each time it restarts. To tell OFBiz which data sources to connect with, add a datasource element declaration to the entityengine.xml configuration file for each database needing an Entity Engine-managed connection. There is no limit to the number of datasource elements you may configure within the entityengine.xml file.

To associate one or more entities with an Entity Engine-managed connection, use the delegator element. This element allows you specify precisely which entities are to be managed per defined connection. There is no limit to the number of delegator elements you may add to the entityengine.xml file.

Entities defined within a delegator element are grouped according to "entity groups", where an "entity group" is a collection of entities defined in an entity group definition file. There is no limit to the number of entities or entity groups supported by the OFBiz Entity Engine.

Creating entity groups

To effectively manage an unlimited number of entities, databases, and database connections, the Entity Engine groups together entities that map to tables on the same physical database. For example, out-of-the-box, all the OFBiz ERP entities that are physically implemented as tables on the Derby database are grouped together in the org.ofbiz entity group.

Getting ready

There is no need to create new entity groups if your entities will be part of the OFBiz schema. You can use the default OFBiz instance group-name of org.ofbiz.

How to do it...

To add more entities that are not part of the Derby entity group, or if you wish to separate out your entities into different entity groups, create new entity groups as shown here:

  1. Within the entityengine.xml file (~framework/entity/config/entityengine.xml), define a delegator for the group with a pointer to the datasource database configuration element. Each delegator is defined with a delegator element as shown here:

    <delegator name="default" entity-model-reader="main"
    entity-group-reader="main" entity-eca-reader="main"
    distributed-cache-clear-enabled="false">
    <group-map group-name="org.ofbiz"
    datasource-name="localpostnew"/>
    <group-map group-name="org.ofbiz.olap"
    datasource-name="localderbyolap"/>
    </delegator>

  2. Within the ofbiz-component.xml file, create one or more entity-resource declarations, each pointing to the location of an entity group definition file as shown here:

    <entity-resource type="group" reader-name="main" loader="main"
    location="entitydef/entitygroup_olap.xml"/>

  3. The entity group definition file must have a valid version number and XSD schema declaration, as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <entitygroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation=
    "http://ofbiz.apache.org/dtds/entitygroup.xsd">

  4. Add one or more entity-group XML declaration statements. Each entity-group declaration adds a single entity to the specified group as shown here:

    <entity-group group="org.ofbiz.olap" entity="CurrencyDimension"/>

  5. Restart OFBiz.

How it works...

OFBiz uses entity groups to collect together all the entities to be accessed using the same Entity Engine-managed connection. The Entity Engine labels and otherwise identifies unique database connections using delegators. Entity groups are primarily a convenience for easily mapping database connections, delegators, and specific entities.

Disabling automatic database checks

There are times when you don't want OFBiz to check the database and/or make entity changes on system startup. For example, you may be using the Entity Engine and OFBiz to retrieve data from a read-only database.

Getting ready

Using an editor or IDE of your choosing, open the Entity Engine configuration file:

~framework/entity/config/entityengine.xml

Apache OfBiz Cookbook Over 60 simple but incredibly effective recipes for taking control of OFBiz
Published: September 2010
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

        Read more about this book      

How to do it...

To change the default Entity Engine startup behavior, follow these simple steps:

  1. For each database (datasource) that you wish to turn checking off, find the following configuration attributes and set them to false:

    check-on-start="false"
    add-missing-on-start="false"

  2. Save and close the file.
  3. Restart OFBiz.

How it works...

Each time OFBiz starts up, it checks the entityengine.xml configuration file to determine which databases to connect to and how to handle missing tables, columns, and/or primary and foreign keys. Setting these values to false tells OFBiz to not perform startup checks or initialization-time repairs on the databases so configured.

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-ofthe-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-typename 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.

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. 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 to do it...

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

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.

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:

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.

Creating a new entity model

The Entity Engine supports an unlimited number of entity definitions spread across an unlimited number of data sources. To create a new entity, sometimes called the entity model, is simply to add the entity's definition to an entity definition file.

Getting ready

Make sure you have an entity definition file defined in an existing ofbiz-component.xml file.

How to do it...

Make sure you have an entity definition file defined in an existing ofbiz-component.xml file.

How to do it...

To create a new entity, follow these steps:

  1. Open an existing entity definition file or create a new one.
  2. Add one or more entity elements as shown here. Please note: XML declarations shown below are for demonstration purposes only. Statements have been removed to make reading easier:

    <entity entity-name="EntityName"
    package-name="org.ofbiz.somePackageName"
    title="My New Entity">
    <!-- Entity definition XML declaration(s) go here.
    Removed for demonstration purposes -->
    </entity>

  3. Each entity element will have one or more of the following declarations added:
    • field element declarations within the entity element:

      <field name="someFieldName" type="any defined type">
      </field>

    • prim-key elements to define the primary key for this entity:

      <prim-key field="primaryKeyField"/>

    • relation elements.
  4. Close the entity element with a closing tag (</entity>).
  5. Save and close the file.
  6. Restart OFBiz to make the entity definitions effective.

How it works...

When OFBiz first starts up, it checks the data model as defined by configured entities in valid entity definition files. Any new entries in entity definition files are checked for configuration integrity and added to the OFBiz data model automatically.

There's more...

The following table summarizes many common entity configuration settings:

To configure one or more primary keys for an entity, add an XML declaration similar to the following:

If performance is an issue, you may optionally configure one or more index elements for an entity. Indexes are configured as shown here:

To configure the following entity behavior for an individual entity, use the settings as shown here:

Field elements map the target database columns to the equivalent OFBiz field definition. To add one or more column definitions as field entries, use the field element with the following attribute settings:

Just as physical database tables may be associated with one another through defined "relationships", OFBiz entities may be related. Use the relation element attribute settings to define one or more relational associations between the defined entity and any other OFBiz entities as described here:

Modifying an existing entity model

To change the entity model where the physical characteristics of the underlying database changes, you must rebuild the target database table as well as modify the corresponding entity model to coincide with these changes.

Note: simply changing an entity's configuration will not reconfigure the physical database tables and column definitions on the target database.

To change an entity model where the existing column structure is not affected, for example, to add a new relationship definition for an entity, may not require first changing the target database

Getting ready

First of all, ensure the following:

  1. If necessary, back up any data in the target database table(s) as modeled by the entity. You may use a third-party database specific backup tool or the OFBiz WebTool's Entity XML Export tool located at:

    https://localhost:8443/webtools/control/xmldsdump

  2. If required, drop the table from the database. You may use the database vendor's tool, third-party database specific tool, or the OFBiz WebTool's Entity SQL Processor. To use the Entity SQL Processor, log in to WebTools and navigate to:
  3. https://localhost:8443/webtools/control/EntitySQLProcessor

    In the SQL Command text box, enter in a command similar to the following SQL DROP statement and click the submit button. Note: do not add a trailing semi-colon. This will drop the indicated table:

    DROP TABLE "tablename"

How to do it...

To modify an existing entity, follow these steps:

  1. For changes that reflect modifications to the physical characteristics of the data on the target database, remove the target table from the database as indicated earlier.
  2. Using any text editor, open the entity definition file containing the entity model to change.
  3. Modify the entity model definition by editing the existing entity definition.
  4. Save and close the entity definition file.
  5. Restart OFBiz to make the modified entity model effective. For tables that have been removed from the target database, OFBiz will attempt to recreate the table using the entity's new definition.
  6. If necessary, reload any data that may have been saved.

How it works...

OFBiz entities are models of physical storage repositories such as databases. When the underlying medium changes, for example, columns within an RDBMS table are reconfigured, OFBiz must be told how to adjust its view of the data. OFBiz cannot rearrange existing data. It can only modify its model of that data.

By default, OFBiz can, however, if configured to do so, instruct a data source to create new tables, fix certain key assignments, and create or remove table relationships based on configured entity models when it starts up.

Building a view-entity

OFBiz view-entities are a convenient way of constructing SQL JOIN statements. They do not force the creation of database tables or map to database tables as do entity definitions, but rather they allow OFBiz to create runtime views of related tables so that users may easily access joined tables without creating complicated SQL JOIN statements.

Getting ready

View-entities are defined in entity definition files along with entity definitions. Make sure you have an entity definition file (to use for any view-entities) configured in an existing ofbiz-component.xml.

How to do it...

The process for creating one or more view-entities is similar to that of creating an entity definition, and can be done by following these steps:

  1. Open an existing entity definition file or create a new one.
  2. Add one or more view-entity elements as shown here. Please note: XML declarations shown below are for demonstration purposes only. Statements have been removed to make reading easier:

    <view-entity entity-name="EntityName"
    package-name="org.ofbiz.somePackageName"
    never-cache="true" title="My New View Entity">
    <!-- Entity definition XML declaration(s) go here.
    Removed for demonstration purposes -->
    </view-entity>

  3. Close the view-entity element with a closing tag (</view-entity>).
  4. Save and close the file.
  5. Restart OFBiz to make the view-entity definition effective.
  6. Use the WebTools Entity Reference - Interactive Version to see if the new view-entity was successfully created (https://localhost:8443/webtools/control/entityref).
  7. Programmatically, view-entities are accessed in the same manner as entities.

How it works...

OFBiz view-entities are a convenient way to create SQL JOIN statements (joining together table column data from two or more tables) using XML declarations instead of SQL statements. The Entity Engine automatically creates the SQL JOIN statement any time the view-entity is accessed.

View-entities may be created or removed from the OFBiz Entity Engine data model at any time using the mentioned recipe. View-entities are read-only, and attempts to store data using a view-entity will fail.

There's more...

An OFBiz view-entity has one or more participating "member" entities. As a convenient way to refer to a member entity, you must create an entity level alias for that member using the member-entity element as shown here:

Each member entity may have one or more field aliases defined for any existing entity field. All fields are referenced using field aliases within the context of the view-entity. To create fieldlevel aliases, use the alias element tag as shown here:

If all fields for an entity should be returned when calling this view-entity, you may use the shortcut alias-all element as defined here:

To create a SQL OUTER JOIN (that is, a JOIN on two tables where records from the joined tables may not have matching pairs), use the view-link element to define the relationship between one entity and another target entity, and set the rel-optional value to true:

Summary

In this article we have learnt the various recipe's for OFBiz Entity Engine in detail.

About the Author :


Ruth Hoffman

An OFBiz evangelist with over 25 years of information technology experience including stints in software design and development, training, project management, product marketing and software sales, Ruth has dedicated the last few years of her eventful career to furthering the OFBiz vision. Whether it be writing about OFBiz, providing implementation and OFBiz consulting services or just “kicking the OFBiz tires” for each new release, she enjoys working with and telling the world about all that this amazing ERP software suite has to offer.
Ruth’s current passion is her quest to provide high quality, cost effective OFBiz training and documentation. She has pursued this quest through multiple channels including the myofbiz.com website. She invites everyone to stop by, take a gander and give some feedback: http://www.myofbiz.com
As a long standing supporter of OFBiz, she has authored several other OFBiz titles including “OFBiz E-Commerce Out-Of-The-Box” and “The OFBiz Catalog Manager” For more information, please see: http://www.myofbiz.com.
Acknowledgements
I’d like to thank my partner, JC,  for being so patient and understanding, my daughter for not getting too upset when I cancelled our planned Lilac Festival adventure and my dog Sadie for sitting by me all those long hours when no one else would.
Also, my sincerest thanks go to Adrian Crum and Shi Jinghai for their dedicated efforts in providing technical review of this book. Thanks for keeping me honest and setting me straight on a number of occasions. Your contributions are greatly appreciated!

Books From Packt


Apache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application Development

Apache Roller 4.0 – Beginner's Guide
Apache Roller 4.0 – Beginner's Guide

Apache MyFaces Trinidad 1.2: A Practical Guide
Apache MyFaces Trinidad 1.2: A Practical Guide

Apache CXF Web Service Development
Apache CXF Web Service Development

Apache Geronimo 2.1: Quick Reference
Apache Geronimo 2.1: Quick Reference

Apache Maven 2 Effective Implementation
Apache Maven 2 Effective Implementation

Apache Struts 2 Web Application Development
Apache Struts 2 Web Application Development

Apache JMeter
Apache JMeter


Your rating: None Average: 4 (1 vote)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
C
T
2
X
9
z
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software