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

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Senior Product Associate - *Apple* Pay (AME...
…is seeking a Senior Associate of Digital Product Management to support our Apple Pay product team. Labs drives innovation at American Express by originating, Read more
Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.