3 Replies Latest reply on Nov 29, 2006 10:30 AM by Peter Johnson

    EJB entity with static final field

    buggs bunny Newbie

      Hi,

      I'm new in EJB, and i'm looking for a way to define constant for entity.
      I have a Client class. This client can be of various type, and i want to define constant for this definition.
      Apparently, the use of static filed in EJB is not a good practice. SO my question is : "How to define this constant?"

      Here is a sollution http://publib.boulder.ibm.com/infocenter/rtnlhelp/v6r0m0/topic/com.ibm.etools.ejb.assembly.doc/topics/tecrteev.html .
      Is there an equivalente in JBOSS 4.0.5?

      In fact i'm not really satisfied by this sollution, so is there another way to do that?

      Should I define my constant in each interface (ClientLocal and Client)?
      Is there a simple solution with xdoclet?

      Thank you for supporting begginer :-)

        • 1. Re: EJB entity with static final field
          Peter Johnson Master

          You used the term "constant". Do you mean something like this:

          public static final String FOO = "something";
          public static final int BAR = 123;

          If so, feel free to use these in EJBs, as static finals (constants) of the basic data types and strings are perfectly acceptable.

          • 2. Re: EJB entity with static final field
            buggs bunny Newbie

            Yes this is exactly how I want too use it.
            Thanks.

            Now, another question:
            Because I will need this "constant" outsite the EJB and because the bean definition is not availlable outside, How can I define this constant?

            I use XDoclet to generate my classes and interfaces. Is there a way to tell it to "export" this static final field in the generated class? (interfaces and dataClasses)? Should I define some function giving me the constant value? (Bhaaaaa! Sound really dirty)

            • 3. Re: EJB entity with static final field
              Peter Johnson Master

              I'm not sure what you are asking, so if the following doesn't answer your question, please rephrase it.

              I typically have a class named Constant, and in the class I place a lot of static final fields. Then in my EJB code, I have statements like:

              MyEjb ejb = (MyEjb)context.lookup(Constant.FOO);

              Of course, I also often place the static final fields in the EJB itself.