-2

i am want output name of columns from table of my database on MySql. I am use Rest on Java, maven, tomcat, hibernate.

this code not work:

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();
}

Please help me, how output name of columns ?

Lev
  • 45
  • 1
  • 1
  • 4

2 Answers2

0

You can use the information_schema database and then use a standard hibernate query definition. In the information schema you can use a query as:

select * from COLUMNS WHERE table_name='person';
Norbert
  • 6,026
  • 3
  • 17
  • 40
0

You can see this post: Get column name of property mapped with Hibernate

((Column) sessionFactoryBean.getConfiguration().getClassMapping(Person.class.getName())
     .getProperty("myProperty").getColumnIterator().next()).getName();

and Get table column names in Hibernate

Matthew Read
  • 1,365
  • 1
  • 30
  • 50
xrcwrn
  • 5,339
  • 17
  • 68
  • 129