Changes for page The EOModel
Last modified by Pascal Robert on 2012/03/10 15:42
From version 20.1
edited by Pascal Robert
on 2011/05/03 22:05
on 2011/05/03 22:05
Change comment:
There is no comment for this version
To version 23.1
edited by Pascal Robert
on 2011/02/27 12:28
on 2011/02/27 12:28
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,5 +3,3 @@ 1 -{{toc}}{{/toc}} 2 - 3 3 = The EOModel = 4 4 5 5 While it may seem obvious, data modeling is perhaps the most important phase of WebObjects application development. Data models form the foundation of your business logic and business logic forms the core of your application. Good business logic is essential to building effective applications, so it follows that good data models are essential to the success of the applications you build. Most importantly, data modeling plays a crucial role in object-relational mapping, the process in which database records are transposed into Java objects. ... ... @@ -13,19 +13,19 @@ 13 13 Data models in Enterprise Objects reflect the earliest decisions about what data users see and how they are allowed to interact with data. This is because an important part of data modeling is deciding which entities and attributes are visible to clients and editable by clients. So by including data modeling as an early part of the design process, you can simplify future implementation details. 14 14 15 15 WebObjects provides you with a great tool for object-relational mapping called EOModeler. It allows you to 16 -■ build data models either from scratch or by analyzing preexisting data sources using reverse engineering 17 -■ add and customize entities (tables) and attributes (columns) 18 -■ form relationships between entities 14 +■ build data models either from scratch or by analyzing preexisting data sources using reverse engineering 15 +■ add and customize entities (tables) and attributes (columns) 16 +■ form relationships between entities 19 19 ■ form relationships across multiple models 20 -■ generate SQL from a model to create or update a data-source schema based on the model 21 -■ generate Java classes from a model in which you can add custom business logic 22 -■ use stored procedures within data models 18 +■ generate SQL from a model to create or update a data-source schema based on the model 19 +■ generate Java classes from a model in which you can add custom business logic 20 +■ use stored procedures within data models 23 23 ■ graphically build fetch specifications for retrieving data 24 -■ flatten attributes and relationships 25 -■ define derived attributes 26 -■ build database queries in raw SQL 22 +■ flatten attributes and relationships 23 +■ define derived attributes 24 +■ build database queries in raw SQL 27 27 28 -In an Entity-Relationship model, distinguishable things are known as entities, each entity is defined by its component attributes, and the affiliations, or relationships between entities, are identified (together, attributes and relationships are known as properties). From these three simple modeling objects ~-~--entities, attributes, and relationships~-~--arbitrarily complex systems can be modeled.26 +In an Entity-Relationship model, distinguishable things are known as entities, each entity is defined by its component attributes, and the affiliations, or relationships between entities, are identified (together, attributes and relationships are known as properties). From these three simple modeling objects—entities, attributes, and relationships—arbitrarily complex systems can be modeled. 29 29 30 30 == Entities == 31 31 ... ... @@ -94,14 +94,14 @@ 94 94 For example, when you specify Number as the value class for a particular attribute, you are telling Enterprise Objects to use java.lang.Number, which is an abstract class. This is where the value type characteristic steps in. It specifies the exact class an attribute should map to. 95 95 The possible value types for numeric attributes are as follows(note case): 96 96 97 -■ b ~-~--java.lang.Byte98 -■ s ~-~--java.lang.Short99 -■ i ~-~--java.lang.Integer100 -■ l ~-~--java.lang.Long101 -■ f ~-~--java.lang.Float102 -■ d ~-~--java.lang.Double103 -■ B ~-~--java.math.BigDecimal104 -■ c ~-~--java.lang.Boolean95 +■ b—java.lang.Byte 96 +■ s—java.lang.Short 97 +■ i—java.lang.Integer 98 +■ l—java.lang.Long 99 +■ f—java.lang.Float 100 +■ d—java.lang.Double 101 +■ B—java.math.BigDecimal 102 +■ c—java.lang.Boolean 105 105 106 106 == Prototypes == 107 107 ... ... @@ -114,7 +114,7 @@ 114 114 ~1. An entity named EOAdaptorNamePrototypes, where AdaptorName is the name of the adaptor for your model. WebObjects 5.2 includes an adaptor for JDBC data sources and an adaptor for JNDI data sources. So you can create a prototype entity called either EOJDBCPrototypes or EOJNDIPrototypes, depending on the adaptor you use. 115 115 2. An entity named EOPrototypes. 116 116 117 -To create a prototype attribute, first create a prototype entity ~-~--an entity named either EOAdaptorNamePrototypes or EOPrototypes~-~--and add an attribute to it. Figure 3-3 shows an attribute in a prototype entity. It shows all the values that prototype attributes can define: column name, value class, external type, and value type.115 +To create a prototype attribute, first create a prototype entity—an entity named either EOAdaptorNamePrototypes or EOPrototypes—and add an attribute to it. Figure 3-3 shows an attribute in a prototype entity. It shows all the values that prototype attributes can define: column name, value class, external type, and value type. 118 118 119 119 To assign a prototype attribute to an attribute, reveal the Prototype column in table mode, and select a prototype attribute from the pop-up menu. The prototype attributes that appear in the pop-up list in the Prototype column include prototype attributes defined in any entity in any model in the application's model group, which includes the current model. 120 120 ... ... @@ -122,13 +122,13 @@ 122 122 123 123 Every relationship has a cardinality. The cardinality defines how many destination records can potentially resolve the relationship. In relational database systems, there are generally two cardinalities: 124 124 125 -■ to-one relationship ~-~--for each source record, there is exactly one corresponding destination record126 -■ to-many relationship ~-~--for each source record, there may be zero, one, or more corresponding destination123 +■ to-one relationship—for each source record, there is exactly one corresponding destination record 124 +■ to-many relationship—for each source record, there may be zero, one, or more corresponding destination 127 127 records 128 128 129 129 An employeeDepartment relationship is an example of a to-one relationship: An employee can be associated with only one department in a company. An Employee entity might also be associated with a Project entity. In this case, there would be a to-many relationship from Employee to Project called projects since an Employee can have many projects. 130 130 131 -The construction of a relationship requires that you designate at least one attribute in each entity as a relationship key. 129 +The construction of a relationship requires that you designate at least one attribute in each entity as a relationship key. 132 132 133 133 There are some general guidelines when choosing which attributes to use as relationship keys. In to-one relationships, the destination key must be a primary key in the destination entity. In to-many relationships, the destination key is usually a foreign key in the destination entity (which is most often a copy of the source entity's primary key). The source key or foreign key should emulate the destination key in that the data types must be the same and the names should be similar. 134 134 ... ... @@ -148,22 +148,22 @@ 148 148 149 149 === Flattened Relations === 150 150 151 -A flattened attribute is a special kind of attribute that you effectively add from one entity to another by traversing a relationship. When you form a to-one relationship between two entities (such as Person and PersonPhoto), you can add attributes from the destination table to the source table. For example, you can add a personPhoto attribute to the Person entity. This is called "flattening" an attribute and is equivalent to creating a joined column ~-~--it allows you to create objects that extend across tables.149 +A flattened attribute is a special kind of attribute that you effectively add from one entity to another by traversing a relationship. When you form a to-one relationship between two entities (such as Person and PersonPhoto), you can add attributes from the destination table to the source table. For example, you can add a personPhoto attribute to the Person entity. This is called "flattening" an attribute and is equivalent to creating a joined column—it allows you to create objects that extend across tables. 152 152 153 153 Flattening attributes is just another way to conceptually "add" an attribute from one entity to another. A generally better approach is to traverse the object graph directly through relationships. Enterprise Objects makes this easy by supporting the notion of key paths. 154 154 The difference between flattening attributes and traversing the object graph (either programmatically or by using key paths) is that the values of flattened attributes are tied to the database rather than the object graph. If an enterprise object in the object graph changes, a flattened attribute can quickly get out of sync. 155 155 156 -For example, suppose you flatten a departmentName attribute into an Employee object. If a user then changes an employee's department reference to a different department or changes the name of the department itself, the flattened attribute won't reflect the change until the changes in the object graph are committed to the database and the data is refetched (this is because flattened attributes are derived attributes ~-~--see "Definition (Derived Attributes)" (page 38) for more details). However, if you're using key paths in this scenario, users see changes to data as soon as they happen in the object graph. This ensures that your application's view of the data remains internally consistent.154 +For example, suppose you flatten a departmentName attribute into an Employee object. If a user then changes an employee's department reference to a different department or changes the name of the department itself, the flattened attribute won't reflect the change until the changes in the object graph are committed to the database and the data is refetched (this is because flattened attributes are derived attributes—see "Definition (Derived Attributes)" (page 38) for more details). However, if you're using key paths in this scenario, users see changes to data as soon as they happen in the object graph. This ensures that your application's view of the data remains internally consistent. 157 157 158 158 Therefore, you should use flattened attributes only in the following cases: 159 159 160 160 ■ If you want to combine multiple tables joined by a one-to-one relationship to form a logical unit. For example, you might have employee data that's spread across multiple tables such as ADDRESS, BENEFITS, and so on. If you have no need to access these tables individually (that is, if you'd never need to create an Address object since the address data is always subsumed in the Employee object), then it makes sense to flatten attributes from those entities into the Employee entity. 161 -■ If your application is read-only. 159 +■ If your application is read-only. 162 162 ■ If you're using vertical inheritance mapping. See "Vertical Mapping" (page 69). 163 163 164 164 === Reflexive Relationships === 165 165 166 -A unique kind of relationship is the reflexive relationship ~-~--a relationship that shares the same source and destination entity. Reflexive relationships are important when modeling data in which an instance of an entity points to another instance of the same entity.164 +A unique kind of relationship is the reflexive relationship—a relationship that shares the same source and destination entity. Reflexive relationships are important when modeling data in which an instance of an entity points to another instance of the same entity. 167 167 168 168 For example, to show who a given person reports to, you could create a separate manager entity. It would be easier, however, to just create a reflexive relationship. The managerID attribute is the relationship's source key whereas employeeID is the relationship's destination key. Where a person's managerID is the employeeID of another employee object, the first employee reports to the second. If an employee doesn't have a manager, the value for the managerID attribute is null in that employee's record. 169 169 ... ... @@ -197,7 +197,7 @@ 197 197 198 198 Relationships can be compound, meaning that they can consist of multiple pairs of connected attributes. You can specify additional pairs of attributes only in the Relationship Inspector. Simply select a second source attribute and a second destination attribute and click Connect a second time. 199 199 200 -A to-one relationship from one foreign key to a primary key must always have exactly one row in the destination entity ~-~--if this isn't guaranteed to be the case, use a to-many relationship. This rule doesn't apply to a foreign key to primary key relationship where a null value for the foreign key in the source row indicates that no row exists in the destination.198 +A to-one relationship from one foreign key to a primary key must always have exactly one row in the destination entity—if this isn't guaranteed to be the case, use a to-many relationship. This rule doesn't apply to a foreign key to primary key relationship where a null value for the foreign key in the source row indicates that no row exists in the destination. 201 201 202 202 To-one relationships must join on the complete primary key of the destination entity. 203 203 ... ... @@ -227,12 +227,12 @@ 227 227 228 228 {{code}} 229 229 230 -EOModelGroup modelGroup = EOModelGroup.defaultGroup(); 231 -EOFetchSpecification fs = modelGroup.fetchSpecificationNamed("MyFetch", "Listing"); 228 +EOModelGroup modelGroup = EOModelGroup.defaultGroup(); 229 +EOFetchSpecification fs = modelGroup.fetchSpecificationNamed("MyFetch", "Listing"); 232 232 fs = fs.fetchSpecificationWithQualifierBindings(dictionary); 233 -NSMutableDictionary dictionary = new NSMutableDictionary(); 234 -dictionary.takeValueForKey("500000", "askingPrice"); 235 -dictionary.takeValueForKey("4", "bedrooms"); 231 +NSMutableDictionary dictionary = new NSMutableDictionary(); 232 +dictionary.takeValueForKey("500000", "askingPrice"); 233 +dictionary.takeValueForKey("4", "bedrooms"); 236 236 dictionary.takeValueForKey("3", "bathrooms"); 237 237 238 238 {{/code}} ... ... @@ -241,8 +241,8 @@ 241 241 242 242 You can set up an EOModel so that Enterprise Objects automatically invokes a stored procedure for these operations on an entity: 243 243 244 -Insert to insert a new object into an entity 245 -Delete to delete an object from an entity 242 +Insert to insert a new object into an entity 243 +Delete to delete an object from an entity 246 246 Fetch All to fetch all objects in an entity 247 247 Fetch w/ PKto fetch the object in an entity with a particular primary key 248 248 Get PK to generate a new primary key for an entity ... ... @@ -273,7 +273,7 @@ 273 273 274 274 == EO Inheritance == 275 275 276 -One of the issues that may arise in designing your enterprise objects ~-~--whether you're creating a schema from scratch or working with an existing database schema~-~--is the modeling of inheritance relationships.274 +One of the issues that may arise in designing your enterprise objects—whether you're creating a schema from scratch or working with an existing database schema—is the modeling of inheritance relationships. 277 277 278 278 In object-oriented programming, it's natural to think of data in terms of inheritance. A Customer object, for example, naturally inherits certain characteristics from a Person object, such as name, address, and phone number. In inheritance hierarchies, the parent object or superclass is usually rather generic so that less generic subclasses of a related type can easily be added. So, in addition to the Customer object, a Client object also naturally derives from a Person object. 279 279 ... ... @@ -315,7 +315,7 @@ 315 315 316 316 ==== Disadvantages of Horizontal Mapping ==== 317 317 318 -Problems may occur when attributes need to be added to the Person superclass. The number of tables that need to be altered is equal to the number of subclasses ~-~--the more subclasses you have, the more effort is required to maintain the superclass.316 +Problems may occur when attributes need to be added to the Person superclass. The number of tables that need to be altered is equal to the number of subclasses—the more subclasses you have, the more effort is required to maintain the superclass. 319 319 320 320 If, for example, you need to add an attribute called middleName to the Person class, you then need to alter its subclasses, Employee and Customer. So if you have deep inheritance hierarchies or many subclasses, this can be tedious. However, if table maintenance happens far less often than fetches, this might be a viable approach for your application. 321 321 ... ... @@ -334,6 +334,7 @@ 334 334 ==== Disadvantages of Single-Table Mapping ==== 335 335 336 336 Single-table mapping results in tables that have columns for all of the attributes of each entity in the inheritance hierarchy. It also results in many null row values. While these aren't really disadvantages, they may conflict with some database design philosophies. 335 + 337 337 338 338 == EOGenerator == 339 339