Search code examples
javahibernateormhibernate-criteria

Using hibernate without mapping to a particular class


I am trying to solve a problem where I want to write some generic Hibernate code to fetch records from an assortment of SQL tables. I need a few columns from each table, and due to some existing tech debt, in some tables the columns are storing same/similar data with slightly different column names.

The approach I am following is using the createSQLQuery API (found on org.hibernate.Session) and passing in some custom queries.

This returns me a list of lists.

[["val 1", "val 2", "val 3"], ["val 4", "val 5", "val 6"]]

I want this to look more like a dictionary with keys that are column names. I can obviously code that myself. However, is it possible to have Hibernate give me that without having to map to specific classes?


Solution

  • Please try setting the ResultTransformer at your query:

    query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    

    this will have a result of type List<Map<String,Object>>