Create native SQL query without creating entity class in SpringBoot

元气小坏坏 提交于 2019-12-11 06:55:29

问题


The fundamental of ORM is mapping with the objects. But, for some reason, I don't want to create objects for running a query.

Is there any way, in which without creating entities (managed classes), I can run a native SQL query?


回答1:


Yes. You can.

Create a method in the repository class with specific query (native query):

@Query(value="select * from emp", nativeQuery=true)
Object getAllFromEmp();

Keep this method in the repository interface and call it from the service class

Or you can use EntityManager object as below

Query q = entityManager.createNativeQuery("SELECT * FROM emp e");
List<Object[]> empObject= q.getResultList();



回答2:


Have a look at createNativeQuery

...
Query query = em.createNativeQuery("select ...");
...

And, I think you can find more about it in this thread: https://stackoverflow.com/a/2110860/672798



来源:https://stackoverflow.com/questions/50956554/create-native-sql-query-without-creating-entity-class-in-springboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!