views:

142

answers:

1

Hi,

I need to retrieve a list of the columns in a table in the order in which they appear. Is this possible using Hibernate? I've got a Hibernate session object, and I don't mind whether I can do it in Hibernate itself, or using JDBC.

The database is MySQL, and the equivalent SQL command I want to execute is:

"SHOW COLUMNS FROM " + tableName

I tried the following and Hibernate complained:

    Table table = object.getClass().getAnnotation(Table.class);
    String tableName = table.name();
    session.beginTransaction();
    // the following line causes an error
    List columns = session.createSQLQuery("SHOW COLUMNS FROM " + tableName).list();

    session.getTransaction().commit();
    session.close();

Thanks

-- edit --

My final working code is as follows.

/**
 * @param Object object A Hibernate POJO
 */
private ArrayList<String> getColumns(Object object)
{
    Table table = object.getClass().getAnnotation(Table.class);
    String tableName = table.name();
    Session session = this.sessionFactory.openSession();

    Statement statement = null;
    ResultSet resultSet = null;

    ArrayList<String> columns = new ArrayList<String>();
    String columnName;

    Connection connection = session.connection();
    try
    {
        statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        resultSet = statement.executeQuery("SHOW COLUMNS FROM " + tableName);
        while (resultSet.next())
        {
            columnName = resultSet.getString("field");
            columns.add(columnName);
        }
    }
    catch (SQLException ex)
    {
        this.logger.fatal("getColumns: Unable to get column names", ex);
        System.exit(1);
    }
    finally
    {
        session.close();
    }

    return columns;
}
A: 

I need to retrieve a list of the columns in a table in the order in which they appear. Is this possible using Hibernate?

Hibernate is not really made for such a need.

I've got a Hibernate session object, and I don't mind whether I can do it in Hibernate itself, or using JDBC.

That's good because I think you will have to use raw JDBC here. So get the underlying JDBC connection from the session and either use DatabaseMetaData or perform your custom query.

Connection conn = session.connection();
...
Pascal Thivent
Sorry, I forgot to mention I'm using Hibernate core 3.3.2 GA. session.connection() is deprecated and session.getConnection() doesn't exist. I read about the Work API, but it doesn't appear to allow me to return the result of performing the work.I didn't know about DatabaseMetaData. Checking the API I can't see a way of returning the columns in the order in which they appear in the database - but then I can't see how to retrieve an array of columns either. How would I use that?
Michael Jones
@MichaelJones: Yes, sorry, that was a typo. And indeed, `session.connection()` is deprecated but it still work (the Work API is for Hibernate 4.0, this gives you some time).
Pascal Thivent
@MichaelJones: To be honest, I really wonder why you need the column in the order they are defined, especially since you are using an ORM. Implementing anything on top of this will be extremely fragile.
Pascal Thivent
@Pascal: OK, thanks. I didn't particularly want to use a deprecated method, but I think I'll have to. I'm writing a method that will serialise POJOs to CSV files. The db I'll import the files into has a basic loader that will ignore an initial line containing column names. It just expects data to be in the same order as the columns are in a table.
Michael Jones
@MichaelJones: Unless you really want to reinvent the wheel, [DbUnit](http://www.dbunit.org/) does already all this.
Pascal Thivent