1

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.

CBH
  • 23
  • 4

1 Answers1

0

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)

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Thanks. I care about the column orders because I'm trying to build web pages to perform CRUD operations on a table (similar to scaffolds in Ruby on Rails). I want the forms on the pages to list the fields in the same order as the columns in the table. – CBH Jul 18 '18 at 19:33
  • I do not know for a fact, but the driver/database may or may not return the same order as defined. I hope the solution will work for you. – ipolevoy Jul 18 '18 at 21:38