Creating a custom query with Spring DATA JPA?

后端 未结 6 1143
旧巷少年郎
旧巷少年郎 2020-12-15 06:07

I\'m working on a project with Spring Data JPA. I have a table in the database as my_query.

I want to create a method which takes a string as a parameter, and then e

6条回答
  •  情话喂你
    2020-12-15 06:16

    Based on @jelies answer, I am using the following approach

    You can create another interface for your custom methods (as example MyQueryCustom) and then implement it as follows.

    public class MyQueryRepositoryImpl implements MyQueryRepositoryCustom {
        @PersistenceContext
        private EntityManager entityManager;
    
        public int executeQuery(String query) {
            return entityManager.createNativeQuery(query).executeUpdate();
        }
    }
    

    This will execute a custom query.

提交回复
热议问题