0

I want to Execute the SQL , which will be constructed completely at runtime and it can be querying any tables in the schema.

Something like

@Repository public interface BaseRepository extends JpaRepository<Object, Integer> {        
    @Query(":dynamicSQL")   
    List<Object> dynamicExecution(@Param("dynamicSQL") String dynamicSQL);           
}

Please suggest how this can be implemented

1

1 Answer 1

0

JpaRepository is not designed to be used like this. If you want to execute dynamic SQL query then use EntityManager. This is bad design but if you still want to do this then use default methods and pass a EntityManager bean like this:

default List<?> dynamicExecution(EntityManager em, String sql) {
    return em.createNativeQuery(sql).getResultList();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.