Discussions

EJB design: EJB constants

  1. EJB constants (3 messages)

    I would like to use a constant in my ejb to be used for indicating a sort order. i.e.:

    public final static String SORT_NAME = "name"
    public final static String SORT_STATE = "state"
    ...

    Where, perhaps, the strings are the column names in the database or something to that extent. I would then want users to be able to access this when doing, say, an ejbFindByWhatever(sort). Obviously, I know how to put a constant into a POJO, so I know how to place it into my EJB.

    My question, however, is where it is appropriate to put the constant declaration. Should it go in the implementation bean, the home interface, the remote interface, or another class entirely? To make the question more general, what if the constant is to be used not only on the home interface (as it would be here), but on the remote interface as well?

    Threaded Messages (3)

  2. EJB constants[ Go to top ]

    In my opinion, you may place the constants wherever you want. If you want those constants to be accessible via both remote interface and home interface, you may create an interface to hold the constants, then make your remote interface and home interface extend it, and your bean class implement it.

    heavyz.
  3. Constants in interfaces[ Go to top ]

    Of course this is just a matter of personal taste but I definetly prefer to refer to the constants in their interface instead of implementing that interface by classes using them.
    The primary reason is that (almost always) those interface with constants are not types. It makes little sense to implement the interface.
    If you use a proper IDE, then implementing the interface will also polute your code-completion popups.
  4. Constants in interfaces[ Go to top ]

    ...prefer to refer to the constants in their interface instead of implementing that interface by classes using them.

    By this, you mean create an interface containing the constants and simply refer to that interface when wishing to use the constants? I.E.:

    public interface SortColumns {
       public final static NAME = "name";
       ...
    }

    public class SomeEntity extends EJBObject {
       ...
       ejbFindByWhatever(String sort) {
          ...
       }
       ...
    }

    and then use it something like this:

    myObj = SomeEntityHome.findByWhatever(SortColumns.NAME);

    This all makes sense, but it does kind of seem overkill to create a whole new interface to provide constants that are associated really with only one object/entity. But then, that's probably one of the trade-offs with EJBs, huh? That and a part of me is soooooo ready for the enumeration functionality in J2SE 5...