4 Replies Latest reply on Feb 19, 2013 3:39 PM by TPD-Opitz

    Regarding Use of ENUM


      I am new to Web-Development And i am trying to Develop a Framework based on MVC Pattern.

      I will be Having a Query.java File ... which will consist of all my Queries. These Queries will be mostly constants <i.e> Will be FINAL.

      Can we put all these Queries in an ENUM ... which is a better Technique . please suggest.
        • 1. Re: Regarding Use of ENUM
          AJ.M wrote:

          Can we put all these Queries in an ENUM ... which is a better Technique . please suggest.
          You can. Whether it's better or not depends on how these queries will be used and if they will be modified. Don't use enums if the values can change, it's not ideal too if the new values can be added. Are you sure the queries will never change?
          Also a data layer in a framework that uses a static list of queries is unusual. Typically you see a dynamic criteria pattern of some sort. Even if this is for your own framework tables it's still worth considering using the more common data access patterns out there.
          • 2. Re: Regarding Use of ENUM
            Values Added or Values change as in .. it will be like .. many Queries will require Parameters ..

            Eg - Select * from table where Col_1 = ? And Col_2 = ?

            And Some will be like .. Select col_1, col_2 from table .... & then where conditions will be build Dynamically..
            • 3. Re: Regarding Use of ENUM
              Then your queries are not static nameable constants and therefore not great candidates for enums.
              As for the rest of your design, I still think it's better to stick with a dynamic query builder approach that doesn't require you to store any queries at all.
              Your data layer should be able to query new tables and new combinations of filtering by building the required query dynamically for a truly flexible framework. If you use JPA then lots of the work is already done for you.
              • 4. Re: Regarding Use of ENUM
                I think the better place to put query strings is in a properties file.

                But you should use enums to provide identifiers for the query strings:
                enum Query{ TABLE1_BY_ID,TABLE2_SUM_BY_CUSTOMER;
                  private final ResourceBundle queryStrings = ResourceBundle.getBundle("QueryStrings"); //->QueryStrings.properties
                  public String getQuery() { return queryStrings.getString(toString());}
                // in the properties file:
                TABLE1_BY_ID = select * from my_table where id = :the_id
                TABLE2_SUM_BY_CUSTOMER = select  customer, sum(ammount) from order group by customer;