I
I
Ivan R2015-04-08 14:47:10
MySQL
Ivan R, 2015-04-08 14:47:10

How to display column names from a table?

It is necessary to display the name of the columns from the table, I create a REST web service using: Java, Maven, TomCat, MySql, Hibernate. So far I did it, but of course it doesn’t work, I suppose I need some kind of HQL query or something.
personDao.java:

public List<Person> getHeaders() {
    List<Person> persons = null;
    Session session = null;

    try {
        session = sessionFactory.openSession();
        session.beginTransaction();
        persons = session.createQuery("SHOW FIELDS FROM person").list();
        session.getTransaction().commit();
    } catch (Exception ex) {
        if (session != null) {
            session.getTransaction().rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return persons;
}

service.java:
@GET
@Path("/getHeaders")
@Produces(MediaType.APPLICATION_JSON)
public List<Person> getHeaders() {
    return personDao.getHeaders();
}

For example, I will attach the code for displaying all the data from the table, it works correctly:
public List<Person> getAllPersons() {
        List<Person> persons = null;
        Session session = null;

        try {
            session = sessionFactory.openSession();
            session.beginTransaction();
            persons = session.createQuery("from Person p").list();
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return persons;
    }

@GET
    @Path("/getAllJSON")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Person> getAllPersonsInJSONroo() {
        return personDao.getAllPersons();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur, 2015-04-08
@neo55

Can I critique a little?
Firstly - apply a stack trace (Although I'm sure that HibernateException jumped out).
Second, read the Javadoc: CreateQuery() Method - Create a new instance of Query for the given HQL query string.
In your working example, you are using HQL. And in a non-working session, you are trying to push SQL specific to MySql databases.
If you want to use SQL, it's probably better to use createSQLQuery().
Thirdly, it will still work - because you are trying to put in an Entity that describes the table, the column headings, of this table. Do you imagine how this is possible? It is not clear what you want to do, and why it is needed. Do you know the names of the columns in the table? And why the hell should a rest-client get them?
But if you really need it, then here's a working example: https://developer.jboss.org/thread/189593?start=0&...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question