TweetFollow Us on Twitter

Build an EOModel

Volume Number: 16 (2000)
Issue Number: 10
Column Tag: Navigation Services

How to Build an EOModel (or look just like one)

by Patrick Taylor and Sam Krishna

Back in 1996, there wasn't much competition for WebObjects. Nowadays however there are dozens of web application servers on the market, each promising that their package will make it oh-so-easy to publish data from your relational database to the World Wide Web.

If you picked some other web application server you generally had a choice of two solutions: tying yourself to a single vendor's database package (the "simple" solution) or embedding SQL into your page template (the not-so "simple" solution). If writing SQL is your idea of a fun Friday night or if you don't believe that object-oriented classes are a spectacular improvement over SQL., then you probably won't fully appreciate EOF's special charms. But even if you aren't 100% sold on the Enterprise Objects Framework, the sexy EOModeler application might still turn your head.

EOModeler, one of Apple's WebObjects development tools, provides you with a simple way to manage a great deal of the complexity associated with databases. EOModeler lets you graphically design your object/data model.

In EOModel, you replace some of your relational database terminology with object-oriented ones. However, the concepts map quite neatly.

EOEntity

An EOEntity represents an actual class in an object model, one which generally stands for a table in a database. Fundamentally, though, an EOEntity represents some piece of information or data that must be captured and represented in your application.

EOAttribute

An EOAttribute represents a column of data in a database table. EOEntities are composed of EOAttributes. An EOAttribute has two intrinsic data types: a class type and an external type. The class type represents the object type (like a java.lang.String or an NSString) that the attribute will be as an enterprise object's (EO) instance variable. The external type represents the kind of database type (like a VARCHAR) that the attribute will be stored as in the database.

EORelationship

An EORelationship represents a relationship between two EOEntities. There are two fundamental types of relationships: to-one and to-many. What this means is that an EO can have either a to-one relationship to a single EO of a particular type or a to-many relationship to potentially various EOs of a particular type. A few variations on both of these fundamental types exists: many-to-many (where an EO can have a relationship to many EOs of a different type, and EOs of the different type can have multiple relationships back to the original type of EOs), reflexive to-one (where an EO has a relationship back to another EO of the same type) and reflexive many-to-many (where an EO can have to-many relationships back to other EOs of the same type). We will only be covering regular to-one, to-many, and, of course, many-to-many relationships in this article.

EOEntities are Composed of EOAttributes and Connected to Other EOEntities Through EORelationships

EOModeler provides a Wizard that can generate an EOModel from a pre-existing database saving you from much of the effort of recreating an object model from scratch. How complete the object model representation of the underlying database is depends on the adaptor and the database, some adaptors like ODBC provide only basic information like entities and attributes, but not relationships or other database-specific features. You will need to manually modify the EOModel to provide the necessary missing features.

If you're familiar with Microsoft Access' graphical modeling tools, you will rather quickly pick up on the graphical modeling mode in EOModel. You aren't limited however to viewing data flows graphically, EOModeler provides developers with two other views: an outline view and a class browser view.You can view additional information about an EOEntity, EOAttribute or EORelationship with an Inspector.

One interesting thing about EOModeler is that it isn't a one-way only tool, it can generate SQL to create a database from an EOModel. For instance you could generate an EOModel from a MSAccess database and then generate the SQL necessary to recreate the database structure in Oracle 8i. Or you could design your object model from scratch in EOModeler and then using the SQL generator build a database structure. Through its adaptors, WebObjects/EOF provides developers with a remarkable degree of database independence.

An Example Application: ProjectManager

To truly understand EOModeler it helps to see it work in an application. ProjectManager, our example application, is intended to assist a Project Manager in allocating resources for the various projects that he or she manages in a company. One way of describing the entities would be:

  • Employees
  • Projects
  • Departments
  • Managers

Real-life Project Managers reading this article will see that we've simplified the number of entities that normally participate in a real project model. However, we can simplify this model even further when we realize that Manager is just a different type of employee and does not need to be its own entity. Further simplified the current set of entities are:

  • Employees
  • Projects
  • Departments

Access, Sigh ...

There are several very good reasons for using MS Access in your WebObjects application. It is omnipresent, practically every business has an Access database. It is cheap especially if you already own MS Office Professional for Windows. It is easier to create a database with it than with many other RDBMS.

However, you are likely to run into some problems when using Access with WebObjects. You need to ensure that you're using a very recent version of the ODBC drivers. In earlier versions of Access, we had problems with Autonumber in applications which weren't read-only. Unlike other RDBMS, Access only passes a bare minimum of information to the EOModeler Wizard and only accepts a bare minimum from EOModeler's SQL generating feature.

This isn't to say that you shouldn't use MS Access (well ...) but it probably won't be simpler than Frontbase or Openbase because you won't have full access to all the features and automation available through EOModeler.

Having identified the entities in this application, we need to make them usable within the ProjectManager object model. Entities are named using the singular form:

  • Employee
  • Project
  • Department

Singular form is used in order to simplify and rationalize the naming of relationships. You will later need to use the plural form, so it's easier to name entities in singular form rather than try to figure out: "What is the plural form of 'Employees'?"

The next thing we do is create the entities in the EOModel. Generally, we have to define three things in order to create an EOEntity: the entity name, the table name, and the class name.

EOModeler's default behaviour is to assign any EOEntity's class name to EOGenericRecord, which is simply a data object that can be used to manipulate an EO's data without going to the trouble of defining a class or class behavior. In this case we'll give the Employee entity the class name Employee.

Entity NameTable NameClass Name
EmployeeEMPLOYEEEmployee

Common practice in EOModeling is to borrow the database convention of naming tables and columns with all upper case characters. While it isn't necessary to maintain the same names, the entity,table and class names are the same for this example.

Once we've finished filling out the required information for the EOEntity, we can start defining the EOAttributes we want to track in each EOEntity. In the Employee entity, we want to track certain things:

  • First Name
  • Last Name
  • Employee Number
  • Address
  • Salary
  • Department
  • Manager
  • Start Date
  • Title

Here's how we model that information:

Employee Entity

  • firstName
  • lastName
  • employeeNumber
  • employeeID
  • streetAddressOne
  • streetAddressTwo
  • city
  • state
  • zipCode
  • salary
  • departmentID
  • startDate
  • title

You'll notice some things right off. First, the Attributes start off lower case but are intercapped since spaces aren't used between words.The names are descriptive to assist in recognition. You can name entities with acronyms or nonsensical names, but you're only punishing yourself. You'll also notice that there isn't always a one-to-one mapping between the identified items and the actual entity.

Naming

Naming in an Object Model is probably the most important thing a WebObjects developer can do. If entities, attributes, and relationships are named well, a developer can develop and debug so much faster than if elements are improperly named.

Some common problems that occur in bad object modeling:

  1. Poorly named or misnamed entities
  2. Abbreviated names of entities
  3. Abbreviated names of attributes
  4. Misnamed attributes
  5. Misnamed relationships

If you can at all avoid them, abbreviations that aren't part of the common vocabulary (like ID) shouldn't appear in your EOModel. Well-named entities, attributes, and relationships make life easier for the developer-so much so that applications that take months to develop could shave several weeks off with strong naming. Good naming is particularly important if you share responsibility for an application with other developers or if someone else may someday inherit your code.

For our application, Address is more complex than what can be represented usefully in a single attribute - because of certain postal conventions, modeling it usefully requires that we use several smaller attributes, like streetAddressOne, streetAddressTwo, etc. (these address rules apply in the United States but may differ in other countries). You will also notice that the employee's department only appears as a departmentID. We have a departmentID instead of actual department attributes because in our application the employee's department information is actually a relationship that an Employee has with a Department. There are two reasons for choosing this: it conserves resources by storing an ID (32 or 64 bits long) rather than multiple potentially redundant entries (16 bits per Unicode character).

When you model a particular attribute, you need to define:

  • attribute name
  • attribute class value
  • attribute external type
  • attribute column name
  • attribute width (if defining an attribute that tracks a string object)

We also define whether or not an attribute is a class property. Class properties are identified with a small diamond to the left hand side of the attribute name. When attributes are identified as class properties they can be manipulated in code. Attributes that are not marked as class properties are handled automatically by the EOF subsystem.

A word on class properties: any primary or foreign keys should not be marked as class properties (ie missing the small diamond). The EOF subsystem should manage any primary or foreign keys automatically. This way a lot of the magic of EOF is used to deal with key generation with the database.

After all the attributes are defined the EOEntity should look like this:


EORelationships

EORelationships represent relationships from one EO to another EO (or groups of EOs). There are two fundamental types of relationships: to-one and to-many. Since our mythical company only assigns employees to a single department, Employee should have a to-one relationship to Department.

Reality-Based Object Modeling

A well-named object model will do neither a developer nor a client any good if the object model does not represent the reality of a business domain as accurately as possible.

Unfortunately there aren't a lot of positive indicators for how reality-based an object model is. However, there is a set of negatives you can watch out for:

  • Primary or foreign keys are class properties
    There should never be a reason for this. This is probably the number one reason why object models in WebObjects get excessively complex and become difficult to maintain.
  • Excessively deep relationship paths
    An example of this would be traversing this kind of relationship chain:
    • Husband->Wife->Sister->Mother->Brother when all you're looking for is:
    • Husband->Wife->Uncle
    Poor models will have an excessively deep relationship graph for a simple piece of information.
  • Entire to-one relationships are flattened into a table For example, if a Project was limited to only two employees per project, you might see a Project poorly modeled like this:
    • Project
    • name
    • clientName
    • employeeOneFirstName
    • employeeOneLastName
    • employeeTwoFirstName
    • employeeTwoLastName

    When you start seeing a lot of duplication of data between tables, it may be time to create a relationship.
  • Excessively difficult-to-explain-or-name relationships and entities This is a little harder to describe, but you'll probably know it when you see it. For example, you may see or create an EOEntity that you cannot name well after thinking about it for 20-30 minutes. Or you may create a Join Entity that has a to-many relationship to a third entity that neither of the entities on either side of the many-to-many relationship needs.

    This list is by no means complete but keeping these factors in mind should help you in the always difficult task of modeling. Keep practicing at it: a well-structured and accurate Object Model with a strong naming convention is worth its weight in gold. Good luck.


Our mythical company has multiple employees in each department so Department will have an inverse to-many relationship back to Employee.


Department and Project entities must track the following types of information:

Department

  • name
  • budget

Project

  • name
  • clientName

Here are pictures of the fully modeled Department and Project entities without relationships:



Employees aren't limited to working on a single project and each project will tend to have several employees assigned to it. Because of this we need to create a more complex type of relationship between Project and Employee than those we've discussed. The type of relationship that represents these complex systems is called a "many-to-many" relationship. Unlike to-One and to-Many relationships, Many-to-Many relationships can't be represented just by an EORelationship.

What is required to create a Many-to-Many relationships is a 'Join Entity'. A Join Entity is a special type of EOEntity that links two entities to each other in a many-to-many relationship. The Join Entity stores the Primary Keys for each of the two joined entities. Following common practice, we'll name the join entity EmployeeProject to show its place in the relationship between Employee and Project.

EmployeeProject will only have two attributes and two relationships. The two attributes will be employeeID and projectID, respectively. The two relationships will be named employee and project, respectively. Here is what the fully modeled EmployeeProject join entity looks like:


ShortCut!

There is a shortcut in EOModeler which simplifies the creation of many-to-many relationships. Here are the steps to the shortcut:

  • Select the two entities you wish to join
  • Select from the Properties menu, 'Join in Many-to-Many relationship' or select the Many-to-Many icon on the toolbar.

After the Join Entity has been created, you will need to fill in the table name and the attributes' column names. It will look something like this:



Once modeling is complete, here is a diagram of what your model should look like with entities, attributes, and relationships:


You have now built an object-relational model without writing a single line of code. No SQL was needed and, for the most part, you could use any database you wanted. The database portion is often the most difficult and painful part of the development process.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.