-2

How to run SQL query in spring boot when my tables' name is dynamic and the number of the columns of the table is also dynamic/varying depending on the requirement.

While using entity class we require to set static table and column names.

Eg-

Table- FunndTransfer_Category1 have columns - id,name,amount,abc

Table- FunndTransfer_Category2 have columns - id,name,amount,xyz

here column name abc,xyz will add at time of table creation when user upload it.

Is there any alternate approach to run query??

  • 1
    You need to explain further. When you say dynamic, do you mean that the names may change/are random (in which case you need to rethink your data model) or that you have some equivalent tables that you wish to query (again, think about normalisation and consolidation) or something completely different, in which case you need to provide more information. [ask] – JoSSte Jan 13 '20 at 07:43
  • If you have dynamic tables JPA is the wrong approach – Simon Martinelli Jan 13 '20 at 08:20
  • Which method should I follow? – Diptanu Basak Jan 13 '20 at 10:28

1 Answers1

1

you can use something as below

     @PersistenceContext
     private EntityManager entityManager;

     List<Object> getData(){
        String tableName = "FunndTransfer_Category1";
        Query query = entityManager.createNativeQuery("select * from "+tableName);
        return query.getResultList();
     }
Jaganath Kamble
  • 506
  • 2
  • 10