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 execute it as a query in the database.
Method:
executeMyQuery(queryString)
As example, when I pass
queryString= "SELECT * FROM my_query"
then it should run that query in DB level.
The repository class is as follows.
public interface MyQueryRepository extends JpaRepository<MyQuery, Long>{
public MyQuery findById(long id);
@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "?1", nativeQuery = true)
public void executeMyQuery(String query);
}
However, it didn't work as I expected. It gives the following error.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''select * from my_query;'' at line 1
Is there any other way, that I could achieve this goal. Thanks in advance
I just want to act on this part. yes there is a way you can go about it without using the @query annotation. what you need is to define a derived query from your interface that implements the JPA repository instance.
then from your repository instance you will be exposed to all the methods that allow CRUD operations on your database such as
with these methods spring data will understand what you are trying to archieve and implement them accordingly.
Also put in mind that the basic CRUD operations are provided from the base class definition and you do not need to re define them. for instance this is the JPARepository class as defined by spring so extending it gives you all the methods.
For more current information check out the documentation at https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
Using EntityManager you can achieve this .
Suppose your entity class is like bellow:
And your query is "select id,name from users where roll_no = 1001".
Here query will return a object with id and name column. Your Response class is like bellow:
Your Response class is like:
here UserObject constructor will get a Object Array and set data with object.
Your query executing function is like bellow :
Here you have to import bellow packages:
Now your main class, you have to call this function. First get EntityManager and call this
getUserByRoll(EntityManager entityManager,String rollNo)
function. Calling procedure is given bellow:Here is the Imports
get
EntityManager
from this way:Now you have data in this userObject.
Note:
query.getSingleResult()
return a object array. You have to maintain the column position and data type with query column position.select id,name from users where roll_no = 1001
query return a array and it's [0] --> id and 1 -> name.
More info visit this thread .
Thanks :)
There is no special support for this. But what you can do is create a custom method with a
String
parameter and in your implementation get theEntityManager
injected and execute it.Possibly helpful links:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations
How to access entity manager with spring boot and spring data
Note: I would reconsider if what you are trying to do is a good idea because it bleeds implementation details of the repository into the rest of the application.
Unfortunately, your approach will not work. If you use
@Query
annotation, you provide one correct concrete query in JPA or native notation per a method.The only part of it you can parameterise are values used in
WHERE
clause. Consider this sample from official doc: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.
This will execute a custom query.