Getting column names from a JPA Native Query

前端 未结 7 965
孤街浪徒
孤街浪徒 2021-01-02 00:33

I have an administrative console in my web application that allows an admin to perform a custom SQL SELECT query on our database.

Underneath, the application is usin

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 01:31

    This code worked for me

    DTO Class :

     public class ItemResponse {
    
     private T item;
    
     public ItemResponse() {
     }
    
     public ItemResponse(T item) {
       super();
       this.item = item;
     }
    
     public T getItem() {
        return item;
    }
    
    public void setItem(T item) {
        this.item = item;
    }
    
    }
    

    Service Class is in the below

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    import org.springframework.stereotype.Service;
    import org.hibernate.transform.AliasToEntityMapResultTransformer;
    
    @Service
    public class ServiceClass{ 
    
    @PersistenceContext
    public EntityManager entityManager;
    
    public ItemResponse exceuteQueryResponse(String queryString) {
    
            ItemResponse itemResponse=new ItemResponse();           
            Query jpaQuery =  entityManager.createNativeQuery(queryString);
            org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)jpaQuery).getHibernateQuery();
          hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
            List> res = hibernateQuery.list();
    
            itemResponse.setItem(res);
            return itemResponse;
    
        }
    
        }
    

提交回复
热议问题