Search code examples
mysqlactivejdbc

Get column order of table in activejdbc


Is there a way to get the columns of a table in the order that they are defined within the database in activejdbc? I tried this:

    User u = new User(); // User extends org.javalite.activejdbc.Model
    Map<String, ColumnMetadata> columns = u.getMetaModel().getColumnMetadata();

However, the returned map has the columns in alphabetical order, not the order that is defined in the table.

I'm using MySQL if that matters.


Solution

  • It uses standard JDBC metadata calls, please see here: Registry#getColumns.

    However this is returned from the JDBC driver, it is added to a Map in the code. As you know, maps are not ordered in any way. Not sure why you would need to get the columns in the same order as they are defined in the database, or if this is even possible.

    If you really want this, you can drop to a DB level:

    Connection con = Base.connection(); 
    // get metadata from the connection any way you want
    

    This way, you can get whatever driver can provide (your mileage is will be limited by the driver implementation)