Is it possible to obtain metadata from a query with JPA?

0

to obtain the data, I have no problem, I can read and manage them, for this I used:

select * from MiTabla;

And to read it:

em = emf.createEntityManager();     
Query query = em.createNativeQuery("select * from MiTabla");
List<Object[]> listaDatos = query.getResultList();

Now, what I need is to get the names of the fields in that query, the complicated thing is that the structure of the query is not linked with JPA, that is, the result is not a structure of a linked table.

Therefore, I do not know if it is possible to obtain the metadata from a query, that is, as well as obtain the data itself, to be able to obtain the metadata. If someone knows something about the subject, well, great.

Before I finish, I use EclipseLink and I found this answer, but I do not know how to apply it.

link

In advance, thank you very much.

    
asked by ferpaxecosanxez 07.03.2017 в 09:52
source

1 answer

2

You can retrieve the data on a map and then access the name of the fields and values in this way:

String strQuery="select * from MiTabla";
Query query = em.createQuery(strQuery);
query.setHint("eclipselink.result-type", "Map");
query.setHint("eclipselink.cursor.scrollable", true);

ScrollableCursor cursor = (ScrollableCursor) query.getSingleResult();
while (cursor.hasNext()) {
    DatabaseRecord record = (DatabaseRecord) cursor.next();
    Vector<DatabaseField> fields = record.getFields();

    for(DatabaseField field : fields) {
        String name = field.getName();
        Object value = record.getValues(field);
    } 
} 
    
answered by 07.03.2017 / 13:41
source