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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.