persistence@glassfish.java.net

Re: Composition, inheritance or queries: best practice

From: Greg Ederer <greg_at_ergonosis.com>
Date: Tue, 07 Nov 2006 12:15:09 -0800
Hi Craig,

Greatly appreciate your input.
The thing to watch out for here is not to use AppUserNature in your persistence coding. This is to avoid artificial constraints on the relational mapping, i.e. requiring a table for the persistent fields in AppUserNature.

For example, don't try to implement the references from User to the others via a Map<String, AppUserNature> or you might run into performance issues. If you keep AppUserNature as an abstract class and implement the persistent fields and methods in the subclasses you should be ok.

Here is my current implementation of AppUser.getNature():

  public AppUserNature getNature(String name)
  {
    Class clazz = null;
    try
    {
      clazz = Class.forName(name);
    }
    catch (ClassNotFoundException ex)
    {
      return null;
    }
   
    for(AppUserNature nature : this.natures)
    {
      if(clazz.isInstance(nature))
      {
        return nature;
      }
    }
   
    return null;
  }


Is this likely to cause performance problems?

AppUserNature has id, appUser, enabled and createdDate properties, which are common to all subclasses.  Should I declare AppUserNature abstract, and I move the implementation of these properties (and related methods) into the subclasses?

If I move all of these properties into the concrete subclasses, would it be just as well to make AppUserNature an interface rather than an abstract class (not sure if JPA supports this) and simply implement the interface in the concrete classes?

Again, thanks for the help.

Cheers,

Greg

My AppUser class has methods addNature(AppUserNature a), getNature(String className) and hasNature(String className).

(This is probably an established design pattern.  But, I don't know what it's called.  Anyone know?)

User is a Composite. The way you interact with a User could be a Facade.

Navigating the resulting web of objects can be a bit cumbersome.  But, this may just be the best way.

You can hide the complexity of navigation via a  Facade pattern that exposes the behavior you want while hiding the implementation.

Regards,

Craig

Again, thanks.

Cheers,

Greg

Craig L Russell wrote:
Hi Greg,

Consider domain classes User, Customer, Vendor. User has a 1:0..1 relationship with Customer and Vendor (a Customer must have a User but a User might not have a Customer). You can navigate from Customer and Vendor to the corresponding User instance. You can navigate from User to Customer and Vendor (might be null in the object model).

Represent each of User, Customer, and Vendor with its own table. Customer and Vendor have a non-null foreign key to User table.

Craig

On Nov 7, 2006, at 8:54 AM, Greg Ederer wrote:

Hi,

I am building a system that has various kinds of users.  All users have first and last names, a username and a password.  Customer users have a shopping cart and orders; vendor users have products and commissions, author users have articles; et cetera.  Any user can be more than one kind of user; e.g., an author can also be a customer and a vendor all at the same time.  What is the best way to architect this using JPA?

Thanks in advance for any suggestions.

Cheers,

Greg
-- 
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|


Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
P.S. A good JDO? O, Gasp!



-- 
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|


Craig Russell

Architect, Sun Java Enterprise System http://java.sun.com/products/jdo

408 276-5638 mailto:Craig.Russell@sun.com

P.S. A good JDO? O, Gasp!




--
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|