How to store @Query sql in external file for CrudRepository?

前端 未结 3 557
遥遥无期
遥遥无期 2020-12-03 07:42

I\'m using spring with CrudRepositorys for database connection.

Now I require a quite long (several lines) sql query that I\'d prefer to ma

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 08:36

    Not sure if it fits your setup, but, this can be done by :

    1) Adding your query to a hibernate mapping file using the tag

    .......
    

    2) Define a hibernate config file that includes the above file using the tag

    
    

    3) Defining a persistence file with a property "hibernate.ejb.cfgfile" that points to the above config file

    
    

    4) Use the above property file to build the EntityManagerFactory

    Now, the above Query can be used in the Repository method :

    @Query(name = "MyQuery", nativeQuery = true)
    [return type] executeMyQuery();
    

提交回复
热议问题