I have the following query
package gt.com.anibal.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import gt.com.anibal.model.Order;
@Repository
public interface OrderDao extends JpaRepository<Order, Long>{
//@Query("SELECT o FROM Order o")
@Query(value = "SELECT * FROM Order o WHERE o.price = ?1", nativeQuery = true) //native query
public List<Order> findOrder(double price);
}
And this is the error
SELECT
*
FROM
Order o WHERE
o.price = ?
10:13 TRACE 13460 --- [nio-9091-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [DOUBLE] - [25.0]
10:13 WARN 13460 --- [nio-9091-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
10:13 ERROR 13460 --- [nio-9091-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : 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 'Order o WHERE o.price = 25.0' at line 1
10:13 ERROR 13460 --- [nio-9091-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
java.sql.SQLSyntaxErrorException: 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 'Order o WHERE o.price = 25.0' at line 1
If I run the query on MySQL, it works fine
SELECT
*
FROM
test.Order o WHERE
o.price = 25.0
but I think the JpaRepository it's creating a space blank between the from and where, when I use JpaRepository in another Entity, the query it's different in my console
package gt.com.anibal.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import gt.com.anibal.model.Persona;
@Repository
public interface PersonaDao extends JpaRepository<Persona, Long> {
}
Here is the result, I don't see the space blank between from and the next line, I don't know if that is the problem or it is another problem.
select
persona0_.id_persona as id_perso1_3_,
persona0_.apellido as apellido2_3_,
persona0_.email as email3_3_,
persona0_.nombre as nombre4_3_,
persona0_.telefono as telefono5_3_
from
persona persona0_
I suggest to use JPA Query Methods for this kind of simple queries.
Your method should be named as follows:
@Repository
public interface OrderDao extends JpaRepository<Order, Long>{
List<Order> findByPrice(double price);
}
This is a more clean code.
If you want to use native queries for further cases, you should replace your "?1" for the name of your method variable name prefixed by a semicolon like this ":price".
When you use nativeQuery = true, it will interpret as a standard SQL that you can execute directly in MySQL. If you remove nativeQuery = true, @Query will interpret as JPQL. Which one are you trying to use?
In your example, MySQL doesn't understand '... order o where o.price... '. Try this:
Query(value = "SELECT * FROM Order WHERE price =?1", nativeQuery = true) //native query
When nativeQuery is true, Order must be the table name and price must be the column name.
what @rortegat suggested is a good way to write simple queries[at least to me]. Now, in your query:
@Query(value = "SELECT * FROM Order o WHERE o.price = ?1", nativeQuery = true)
Here *
means rows/records of all columns but your method is returning List<Order>
which is not compatible. Instead, change your query like this: @Query(value = "SELECT o FROM Order o WHERE o.price =?1", nativeQuery = true
)
Here, SELECT o FROM Order o
this part means you are returning Order instance from your query by your alias o
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.