What Is an Entity Object? | Home |
Solution Area |
Contents |
Entity objects are classes that encapsulate the business model, including rules, data, relationships, and persistence behavior, for items that are used in your business application. For example, entity objects can represent
the logical structure of the business, such as product lines, departments, sales, and regions
business documents, such as invoices, change orders, and service requests
physical items, such as warehouses, employees, and equipment
From an object-oriented perspective, an entity object represents an object in the real-world problem domain. From a relational database perspective, an entity object provides a Java representation of data from a database table. Advanced programmers can map entity objects to other types of data sources, such as spreadsheets, flat files, and XML files.
Depending on how you want to work, you can automatically create entity objects from existing database tables or define entity objects and use them to automatically create database tables.
The best place to write your business logic is in entity objects, because they consistently enforce business logic for all views of data, accessed through any type of client interface. Business logic includes the following items:
business rules and policy When adding or modifying data, you can ensure that the data complies with your organizations' procedures before adding it to the database. For example, you could increase the salary when an employee is promoted, give an employee three weeks of vacation after they have been at a company three years, or change the status of an order to shipped after all items in an order have been mailed to a customer.
validation logic When adding new data, you can ensure that the data is valid before storing it in the database. For example, you could ensure that a job code is a valid job code.
deletion logic You can make sure that data is deleted only when appropriate and that any dependencies are handled. For example, you could prevent an on-leave employee from being removed.
calculations You can efficiently perform data calculations in the business logic tier. For example, you could calculate an employee's monthly pay based on an hourly rate.
Business logic in an entity object provides immediate feedback to the user if changes are inappropriate. This way, the in-memory business model always remains consistent.
When you use a Business Components for Java wizard to create entity objects from existing tables (reverse generation), it creates one entity object for each database table. It creates an entity object attribute for each column in the database table; each attribute can have the same name as the column or a different name that is more meaningful to your business application. The attribute definitions of an entity object reflect and enforce the properties of the respective database columns, including data types, column constraints, and precision and scale specifications.
An entity object can have an attribute for each column or you can use a subset, for example, if you don't need to work with that column or if a table contains information for more than one entity.
You can also use Business Components for Java wizards to define entity objects, and their attributes, without starting from an existing database table (forward generation). When you use a wizard to generate database tables from entity objects, it creates a table for each entity object, a table column for each entity attribute, and column constraints based on the entity attribute settings. In addition, you can use the Entity Constraint Wizard to define table constraints.
Usually, one entity object maps to one database table. However, a table could store multiple categories of information, so you might need to map multiple entity objects to it.
You can apply different entity objects to different rows. For example, an Items table might store inventory items, requisition items, order items, and so on, in different rows of the table. For each type of row, you would define one type of entity object. In this case, you could have an Item entity object, and InventoryItem, RequisitionItem, and OrderItem entity objects that extend it. In addition, for each entity object, you would define one or more attributes to be a discriminator so the framework can automatically determine which rows map to which entity objects, and manage entity object storage. You would omit columns that do not apply to the entity object you are defining; however, each entity object must always include the primary key.
You should never map multiple entity objects to the same row.
You can create and edit entity objects by using the Entity Object Wizard and Editor. For reverse generation, you can also use the Business Components Project Wizard or Package Wizard or Editor to create default entity objects. Default entity objects contain attributes for each column in the table; you can customize default entity objects in the Entity Object Editor.
During runtime, each entity object instance represents a row in the database table and stores its data. There is only one instance per row, and all instances of the same entity object class within the same transaction are cached together. Multiple view object queries returning the same row refer to the same entity object instance, so updates are visible to all view objects; one entity object can be used by multiple view objects. Each entity object instance is uniquely identified by its primary key attribute or attributes. For more detail, see How Does the Business Logic Tier Cache Data?
For security reasons, entity objects are not exposed to clients. Instead, clients access an entity objects data through one or more view objects. In the view row class, you can make entity object methods available to a client by writing a view row method that calls the method you want to expose. For example, you could use this feature to implement security: you could create different view objects so different clients have varying levels of access to entity object methods.
Relationships between entity objects are expressed through associations. They match attributes, typically key fields, from source and destination entity objects. Accessor methods that return rows and row sets through the association are available on the entity object class.