Announcement Announcement Module
Collapse
No announcement yet.
Handling reference tables within Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Handling reference tables within Spring

    Hi all,

    Not sure if this is the best forum for this question, so hopefully you'll bear with me. I'm wondering how people handle reference tables in their applications where the values need to come from the database. For example, I might have an 'Order' object with a 'status' attribute. The valid statuses are stored in the database in an 'OrderStatus' table with 'code' and 'description' fields. The 'Order' table then has the code in it to track the status. Assume for now that I want the 'Order' object to have an 'OrderStatus' object rather than just a String with the code.

    Now traditionally, I might have modelled the OrderStatus object as a typesafe enumeration, loading in the valid values from the database in a static initialiser (or I could load it lazily if I preferred). The class would then provide a static method to retrieve the relevant object given the code.

    How do people translate this to Spring? When is the best time to load the values from the database, and how would one do it without polluting the OrderStatus class with datasource references?

    Geoff

  • #2
    I haven't done this with spring yet.
    Normally I would advise to make OrderStatus an interface and hide the initialization code within the implementing class. Unfortunately with enumeration types this is not that easy.

    What I can think of is providing an own implementation of FactoryBean which is parameterized with a DataSource and whatever you need to retrieve the required data from your database. This factory could then be able to initialize an instance of your enumeration type.

    Hope that helps,
    Andreas

    Comment


    • #3
      Hi Andreas,

      Thanks for the reply. Making a factory bean had crossed my mind, but the convenience of calls like 'OrderStatus.getStatus(code)' is then lost, since I'd have to go get a reference to the relevant factory bean every time I wanted to use an enumeration. It may come down to that, but I'm wondering if anyone else has a better solution.

      Geoff

      Comment


      • #4
        Hi Geoff,

        actually I meant a different thing. A FactoryBean cannot be accessed from the context directly, rather it provides an instance of another type. So you could create an implementation of FactoryBean which instantiates an OrderStatus instance.
        On accessing the bean, that will be transparent for you. You never get a FactoryBean instance out of a configuration file (See API documentation of the FactoryBean interface).

        For example accessing a declared SimpleRemoteStatelessSessionProxyFactoryBean does not yield an instance of that type. It rather provides you a proxy instance for the parameterized EJB interface.

        Your factory could work the same, except that it accesses the database and returns an OrderStatus.

        Regards,
        Andreas

        Comment


        • #5
          Ah, I think I understand what you're saying. You're talking about getting objects in a manner similar to the way I might get a DataSource out of a JNDI directory using the JndiObjectFactoryBean, aren't you?

          I'm a little confused as to how this relates to an enumerated type though. Is it possible to pass a parameter to such a bean (say, an order number) and have it do something with that at a stage after the initial wiring up of the beans is done? I should mention that in my implementation, the equivalent of the Order object is not a Spring managed bean. It's just a POJO.

          Thanks again for your help.

          Geoff

          Comment


          • #6
            Originally posted by luxaeterna
            Ah, I think I understand what you're saying. You're talking about getting objects in a manner similar to the way I might get a DataSource out of a JNDI directory using the JndiObjectFactoryBean, aren't you?
            Exactly

            Originally posted by luxaeterna
            I'm a little confused as to how this relates to an enumerated type though. Is it possible to pass a parameter to such a bean (say, an order number) and have it do something with that at a stage after the initial wiring up of the beans is done? I should mention that in my implementation, the equivalent of the Order object is not a Spring managed bean. It's just a POJO.
            My understanding of the situation is the following (correct me if you intended it differently):
            - The OrderStatus type contains all states any order can be in. The type is an enumeration type which values are initialized from the database once.
            - An order has exactly one OrderStatus at any given time.

            If that is true, I don't think that it is necessary to alter OrderState instances at runtime. Also the order number should not matter, since it is part of the Order, not of its state.

            Regards,
            Andreas

            Comment


            • #7
              Hi Andreas,

              Your understanding is correct, however, I'm still not sure how you're suggesting I use a FactoryBean. As I understand FactoryBeans (and my use is limited to the JNDI example I quoted earlier, so my understanding may be less than perfect), they are used to allow Spring to manage an object which is returned by a factory method.

              So are you suggesting that I have Spring manage a factory for the various OrderStatus objects? If this is the case, then I don't really need a FactoryBean, since a normal bean would be able to do that. I could just create a singleton bean which maintained a list of the various OrderStatus objects and allowed me to retrieve them based on a code. But again, since I would need to reference this bean from the Order object (which is not Spring managed), I'd need to get references to this bean from my domain objects, coupling them to the container.

              Or are you suggesting something else?

              Thanks again.

              Geoff

              Comment


              • #8
                Originally posted by luxaeterna
                So are you suggesting that I have Spring manage a factory for the various OrderStatus objects? If this is the case, then I don't really need a FactoryBean, since a normal bean would be able to do that.
                That is correct. Indeed I thought of a different approach. I must confess that a true typesafe enumeration implementation might not be achievable. However, something similar might be possible.

                You could do the following in your FactoryBean:
                1) Load your state information from the database
                2) Register the state instances via static methods on OrderStatus

                If pre-instantiation is enabled you wouldn't even need to access OrderStatus from the BeanFactory. Just access the registered state instances via a static accessor on OrderStatus.

                In order to prevent manipulation of the static state of OrderStatus I would add a flag which prevents further changes of the registered instances. That might be set after step 2) above.

                The main thing you cannot achieve is access via distinct static getters per instance. But that cannot work in your case anyway, since the instances are defined from data within the database.

                Regards,
                Andreas

                Comment


                • #9
                  Makes sense to me. This would also allow the use of the convenient 'OrderStatus.getStatus(code)' to obtain instances. Thanks for all your input on this, Andreas. Much appreciated.

                  If anyone else has any alternative approaches, I'd still be interested to hear them.

                  Geoff

                  Comment

                  Working...
                  X