Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: DBRE Custom field names

  1. #1
    Join Date
    Nov 2011
    Location
    Glasgow, Scotland
    Posts
    18

    Default 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. #2
    Join Date
    Nov 2011
    Location
    Glasgow, Scotland
    Posts
    18

    Default

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

  3. #3
    Join Date
    Nov 2011
    Location
    Glasgow, Scotland
    Posts
    18

    Default

    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?

  4. #4
    Join Date
    Jan 2007
    Posts
    18

    Default

    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 at 12:17 PM. Reason: added link to snapshot build

  5. #5
    Join Date
    Mar 2013
    Posts
    6

    Default

    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?

  6. #6
    Join Date
    Jan 2010
    Location
    Mislata - Valencia - Spain
    Posts
    163

    Default

    Quote 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 !
    Mario Martínez Sánchez
    Project Manager & Software Architect
    --------------------------
    Disid Technologies S.L.
    http://www.disid.com
    --------------------------
    gvNIX
    http://gvnix.googlecode.com
    http://www.gvnix.org

  7. #7
    Join Date
    Jan 2007
    Posts
    18

    Default

    Quote 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.

  8. #8
    Join Date
    Mar 2013
    Posts
    6

    Default

    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.

  9. #9
    Join Date
    Jan 2007
    Posts
    18

    Default

    Quote 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.

  10. #10
    Join Date
    Apr 2013
    Posts
    3

    Unhappy DBRE Not working for me...

    Quote 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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •