Announcement Announcement Module
Collapse
No announcement yet.
DBRE Custom field names Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DBRE Custom field names

    Is it possible to override the naming strategy that Roo dbre uses when creating Roo_DbManaged ITDs?

    For example, we have a database which uses 2 char prefixes on column names to indicate the datatype, e.g. TX_EMPLOYEE_NAME, which by default maps to txEmployeeName - I would like it to map to employeeName.

    Also, ID_EMPLOYEE maps to idEmployee, whereas I would rather it was employeeId.

  • #2
    I meant to add, I'm using Roo 1.2.0.M1

    Comment


    • #3
      Ok, it looks likes this isn't currently possible.

      I was hoping for something like Hibernate uses:
      http://docs.jboss.org/tools/2.1.0.Be...eveng-strategy

      However, after doing some investigation, I found that the Roo mapping for columns names is hard-coded in the following method:
      DbreTypeUtils.getName():

      private static String getName(final String str, final boolean isField) {
      StringBuilder result = new StringBuilder();
      boolean isDelimChar = false;
      for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (i == 0) {
      if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
      result.append(isField ? "f" : "T");
      result.append(c);
      } else {
      result.append(isField ? Character.toLowerCase(c) : Character.toUpperCase(c));
      }
      continue;
      } else if (i > 0 && (c == '_' || c == '-' || c == '\\' || c == '/') || c == '.') {
      isDelimChar = true;
      continue;
      }

      if (isDelimChar) {
      result.append(Character.toUpperCase(c));
      isDelimChar = false;
      } else {
      if (i > 1 && Character.isLowerCase(str.charAt(i - 1)) && Character.isUpperCase(c)) {
      result.append(c);
      } else {
      result.append(Character.toLowerCase(c));
      }
      }
      }
      if (ReservedWords.RESERVED_JAVA_KEYWORDS.contains(res ult.toString())) {
      result.append("1");
      }
      return result.toString();
      }
      Maybe a possible enhancement for a future version?

      Comment


      • #4
        Ive been able to get some of this to work with some tweaks to addon-dbre here: https://github.com/chorrylan/spring-...re-tablenaming

        The particular use cases and issues I had to deal with are:
        1) plural tablenames (which results in plural entity names and then weird double-pluralization of subsquent bits)
        3) prefixes on table names that have no meaning to the domain model (eg MV_ for materialized views)
        4) legacy schema with mixed and inconsitent naming conventions (but difficult to change)
        5) problems renaming entities if the ui tier has already been generated and modified so would like to get them right the first time

        Dealing with plural table names is by arbitrarily singularizing all table names on the assumption that it's harmless if the name is already singular or that there's not already a table with a clashing name (the dbre code then suffixes one of them to resolve the clash but it could be confusing) .

        Schema and site-specific configuration is through an extra arg --tableNameMapper <file> for dbre reverse engineer
        This can be a property file in classic or xml format with mapping rules of the form oldname=newname
        eg

        Code:
        # clean up a specific materialized view
        MV_EMPLOYEES=EMPLOYEE
        # or a regex to clean them all up
        (?\:MV_)(.*)=$1

        or a groovy script that implements whatever logic is neccessary in a method of the for String getName(String)
        eg
        Code:
        String getName(String str) {
        // strip off MV_ prefixes from materialized views
         str = str.replaceFirst(/^MV_(.*)/,/$1/)
        // expand abbreviations
         str = str.replaceAll(/((?:_)|(?:^))(ATTRIB)((?:_)|(?:$))/,/$1ATTRIBUTE$3/)
        // etc
         return str
        }

        The groovy script option obviously has the most potential and I was a bit surprised that it worked (as it conflicts with aspectj in the generated application) but as roo itself doesn't use aspects it seems to be ok.

        For anyone interested in trying this without needing to build roo from source there's a snapshot build here: http://www.uniqcon.com.au/spring-roo...D-SNAPSHOT.zip
        Last edited by chorrylan; Apr 15th, 2012, 12:17 PM. Reason: added link to snapshot build

        Comment


        • #5
          Hi - I downloaded the snapshot build and pointed Spring Tool Suite to it. After opening the Roo shell, it updated my POM file (as expected). I then had to manually add the annotations jar into my maven repository and then no compile errors. I then tried to run the reverse engineer with the new tableNameMapper option but the roo shell just returns to the prompt with no action and no errors.
          Any ideas?

          Comment


          • #6
            Originally posted by ammwhite View Post
            Is it possible to override the naming strategy that Roo dbre uses when creating Roo_DbManaged ITDs?
            Override de naming strategy is not currently possible.
            But next to reverse engineer, you can rename your entities class name.

            Regards !

            Comment


            • #7
              Originally posted by graham777 View Post
              but the roo shell just returns to the prompt with no action and no errors.
              Any ideas?
              Can you try it from the cmdline rather than inside sts to see if it behaves differently?
              I gather sts has a modified roo shell that may be conflicting.

              Comment


              • #8
                Thanks - that solved it.

                Although interestingly I didn't need to supply the --tableNameMapper parameter. This version just removed the plural from the table name when creating the entities.

                Comment


                • #9
                  Originally posted by graham777 View Post
                  I didn't need to supply the --tableNameMapper parameter.
                  yep. The --tableNameMapper extension is for other oddities and extensions.
                  Handling tablenames in plural form or even mixed singlular/plural form should be automatic.

                  Comment


                  • #10
                    DBRE Not working for me...

                    Originally posted by chorrylan View Post
                    yep. The --tableNameMapper extension is for other oddities and extensions.
                    Handling tablenames in plural form or even mixed singlular/plural form should be automatic.
                    From what I can see you've managed to get the DBRE working. I'm having trouble getting it to work. Could you point me in the right direction to get started so that I can get DBRE working.??

                    Thanks!!

                    Comment


                    • #11
                      Can you give some background - Are you using Roo within Spring Tool Suite or from Command Prompt? What database are you trying to reverse engineer? What commands have you tried? Did you create your project from Roo?

                      Comment


                      • #12
                        DBRE not working for me.....

                        Originally posted by graham777 View Post
                        Can you give some background - Are you using Roo within Spring Tool Suite or from Command Prompt? What database are you trying to reverse engineer? What commands have you tried? Did you create your project from Roo?
                        I hope you were replying to my post... Here's a little background like you asked for...

                        Roo from the command line... It's a MYSQL - HIBERNATE database. And I've tried the standard commands for the reverse engineering process. I continually get a response that there is no driver available to do perform the reverse engineer process. And there is no download or plugin install options available either.

                        Comment


                        • #13
                          Yes I was replying.
                          Have you run the following commands in Roo:
                          --> persistence setup --provider HIBERNATE --database MYSQL

                          This will create a database.properties file and you then need to edit this to add the connection/driver details to get to your MYSQL database.

                          Then you should be able to do the reverse engineer command.

                          One other note is that I am connecting to SQL Server and I had to add the JTDS driver to the roo shell by downloading jtds-1.2.4.jar and then running the following command in Roo:
                          --> osgi start --url file:///C:\[path]\jtds-1.2.4.jar

                          Comment

                          Working...
                          X