Get dynamic SQL column names from Hibernate












5















I have an Oracle table that has a CLOB in it. Inside this CLOB can be a SQL statement. This can be changed at any time.



I am currently trying to dynamically run these SQL statements and return the column names and data back. This is to be used to dynamically create a table on the web page.



Using Hibernate, I create the query and get the data like so:



List<Object> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();


This gets the data I need, but not the column names. I have tried using the getReturnAliases() method, but it throws an error that the "java.lang.UnsupportedOperationException: SQL queries do not currently support returning aliases"



So my question is: Is there a way through Hibernate to get these values dynamically?










share|improve this question























  • Please refer to stackoverflow.com/questions/2605385/… and see if it fills your need

    – user3487063
    Aug 4 '14 at 19:11











  • Want to place your comment into an answer so I can accept it? :)

    – Ascalonian
    Aug 5 '14 at 11:37











  • Sure, thank you :)

    – user3487063
    Aug 5 '14 at 13:06
















5















I have an Oracle table that has a CLOB in it. Inside this CLOB can be a SQL statement. This can be changed at any time.



I am currently trying to dynamically run these SQL statements and return the column names and data back. This is to be used to dynamically create a table on the web page.



Using Hibernate, I create the query and get the data like so:



List<Object> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();


This gets the data I need, but not the column names. I have tried using the getReturnAliases() method, but it throws an error that the "java.lang.UnsupportedOperationException: SQL queries do not currently support returning aliases"



So my question is: Is there a way through Hibernate to get these values dynamically?










share|improve this question























  • Please refer to stackoverflow.com/questions/2605385/… and see if it fills your need

    – user3487063
    Aug 4 '14 at 19:11











  • Want to place your comment into an answer so I can accept it? :)

    – Ascalonian
    Aug 5 '14 at 11:37











  • Sure, thank you :)

    – user3487063
    Aug 5 '14 at 13:06














5












5








5








I have an Oracle table that has a CLOB in it. Inside this CLOB can be a SQL statement. This can be changed at any time.



I am currently trying to dynamically run these SQL statements and return the column names and data back. This is to be used to dynamically create a table on the web page.



Using Hibernate, I create the query and get the data like so:



List<Object> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();


This gets the data I need, but not the column names. I have tried using the getReturnAliases() method, but it throws an error that the "java.lang.UnsupportedOperationException: SQL queries do not currently support returning aliases"



So my question is: Is there a way through Hibernate to get these values dynamically?










share|improve this question














I have an Oracle table that has a CLOB in it. Inside this CLOB can be a SQL statement. This can be changed at any time.



I am currently trying to dynamically run these SQL statements and return the column names and data back. This is to be used to dynamically create a table on the web page.



Using Hibernate, I create the query and get the data like so:



List<Object> queryResults = null;
SQLQuery q = session.createSQLQuery(sqlText);
queryResults = q.list();


This gets the data I need, but not the column names. I have tried using the getReturnAliases() method, but it throws an error that the "java.lang.UnsupportedOperationException: SQL queries do not currently support returning aliases"



So my question is: Is there a way through Hibernate to get these values dynamically?







java sql hibernate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 4 '14 at 18:46









AscalonianAscalonian

8,400155895




8,400155895













  • Please refer to stackoverflow.com/questions/2605385/… and see if it fills your need

    – user3487063
    Aug 4 '14 at 19:11











  • Want to place your comment into an answer so I can accept it? :)

    – Ascalonian
    Aug 5 '14 at 11:37











  • Sure, thank you :)

    – user3487063
    Aug 5 '14 at 13:06



















  • Please refer to stackoverflow.com/questions/2605385/… and see if it fills your need

    – user3487063
    Aug 4 '14 at 19:11











  • Want to place your comment into an answer so I can accept it? :)

    – Ascalonian
    Aug 5 '14 at 11:37











  • Sure, thank you :)

    – user3487063
    Aug 5 '14 at 13:06

















Please refer to stackoverflow.com/questions/2605385/… and see if it fills your need

– user3487063
Aug 4 '14 at 19:11





Please refer to stackoverflow.com/questions/2605385/… and see if it fills your need

– user3487063
Aug 4 '14 at 19:11













Want to place your comment into an answer so I can accept it? :)

– Ascalonian
Aug 5 '14 at 11:37





Want to place your comment into an answer so I can accept it? :)

– Ascalonian
Aug 5 '14 at 11:37













Sure, thank you :)

– user3487063
Aug 5 '14 at 13:06





Sure, thank you :)

– user3487063
Aug 5 '14 at 13:06












4 Answers
4






active

oldest

votes


















10














You can use :



q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();


to get column names in createSQLQuery.



For more details please refer to this question.






share|improve this answer

































    1














    You can use the addScalar method to define the columns.



    Look at 16.1.1
    https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html






    share|improve this answer































      1














      You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.






      share|improve this answer































        0














        In 2018 I would suggest using NativeQueryTupleTransformer with native queries.



        query.setResultTransformer(new NativeQueryTupleTransformer());


        The result format is List<Tuple>. This format is very convenient to work with native SQL queries.






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25125480%2fget-dynamic-sql-column-names-from-hibernate%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          10














          You can use :



          q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
          List<Map<String,Object>> aliasToValueMapList=query.list();


          to get column names in createSQLQuery.



          For more details please refer to this question.






          share|improve this answer






























            10














            You can use :



            q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
            List<Map<String,Object>> aliasToValueMapList=query.list();


            to get column names in createSQLQuery.



            For more details please refer to this question.






            share|improve this answer




























              10












              10








              10







              You can use :



              q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
              List<Map<String,Object>> aliasToValueMapList=query.list();


              to get column names in createSQLQuery.



              For more details please refer to this question.






              share|improve this answer















              You can use :



              q.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
              List<Map<String,Object>> aliasToValueMapList=query.list();


              to get column names in createSQLQuery.



              For more details please refer to this question.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 23 '17 at 12:31









              Community

              11




              11










              answered Aug 5 '14 at 13:19









              user3487063user3487063

              2,6611920




              2,6611920

























                  1














                  You can use the addScalar method to define the columns.



                  Look at 16.1.1
                  https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html






                  share|improve this answer




























                    1














                    You can use the addScalar method to define the columns.



                    Look at 16.1.1
                    https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html






                    share|improve this answer


























                      1












                      1








                      1







                      You can use the addScalar method to define the columns.



                      Look at 16.1.1
                      https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html






                      share|improve this answer













                      You can use the addScalar method to define the columns.



                      Look at 16.1.1
                      https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 4 '14 at 19:11









                      Anthony_MichaelAnthony_Michael

                      424




                      424























                          1














                          You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.






                          share|improve this answer




























                            1














                            You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.






                            share|improve this answer


























                              1












                              1








                              1







                              You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.






                              share|improve this answer













                              You could implement a ResultTransformer ( http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/transform/ResultTransformer.html ) and set it on the native query. I think with a native SQL query you get the aliases as specified in the SQL as alias parameter in the callback method.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 4 '14 at 21:27









                              Maarten WinkelsMaarten Winkels

                              2,1541314




                              2,1541314























                                  0














                                  In 2018 I would suggest using NativeQueryTupleTransformer with native queries.



                                  query.setResultTransformer(new NativeQueryTupleTransformer());


                                  The result format is List<Tuple>. This format is very convenient to work with native SQL queries.






                                  share|improve this answer




























                                    0














                                    In 2018 I would suggest using NativeQueryTupleTransformer with native queries.



                                    query.setResultTransformer(new NativeQueryTupleTransformer());


                                    The result format is List<Tuple>. This format is very convenient to work with native SQL queries.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      In 2018 I would suggest using NativeQueryTupleTransformer with native queries.



                                      query.setResultTransformer(new NativeQueryTupleTransformer());


                                      The result format is List<Tuple>. This format is very convenient to work with native SQL queries.






                                      share|improve this answer













                                      In 2018 I would suggest using NativeQueryTupleTransformer with native queries.



                                      query.setResultTransformer(new NativeQueryTupleTransformer());


                                      The result format is List<Tuple>. This format is very convenient to work with native SQL queries.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 23 '18 at 13:12









                                      XaltotunXaltotun

                                      1096




                                      1096






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25125480%2fget-dynamic-sql-column-names-from-hibernate%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin