Getting column names from a JPA Native Query

前端 未结 7 1126
孤街浪徒
孤街浪徒 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:32

    cast query to hibernate query, then use hibernate method

              //normal use, javax.persistence.Query interface
        Query dbQuery = entityManager.createNativeQuery(sql);
        //cast to hibernate query
        org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)dbQuery)
                .getHibernateQuery();
        hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    
        List> res = hibernateQuery.list();
    
        List txTestModels = new ArrayList<>();
        res.forEach(e->{
            TxTestModel txTestModel = new ObjectMapper().convertValue(e, TxTestModel.class);
        //  txTestModels.add(new TxTestModel().setIdd((Integer) e.get("idd")).setMmm((String) e.get("mmm")).setDdd((Date) e.get("ffffd")));
            txTestModels.add(txTestModel);
        });
        System.out.println(txTestModels.size());
    

提交回复
热议问题