Answer the question
In order to leave comments, you need to log in
How to execute a string using JpaRepository?
I have a complex compound WHERE query,
I need to substitute it in the JpaRepository, but when I substitute it, the JpaRepository adds quotes
@Repository
public interface ApartmentRepository extends JpaRepository<Apartment, Long>{
@Query(value = "SELECT COUNT(*) FROM `apartment` WHERE 1=1 :query", nativeQuery = true)
long countByQuery(@Param("query") String query);
}
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 '' AND city = 1'' at line 1
Answer the question
In order to leave comments, you need to log in
What you are trying to do is called SQL injection. Read for interest how many sites were hacked just because it was possible to substitute some string in an SQL query and thus affect its result.
Therefore, JPA (and indeed Hibernate) will not allow you to do this. Yes, you don't need this. Formulate normally the problem you want to solve and ask another question - get advice on how to do it without such perversions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question