无法将位置参数传递到 HQL 查询中
Unable to pass positional parameters into HQL query
在尝试为我的应用程序创建一个 API 时,我尝试进行一个查询,该查询接受一个传入的值,然后 returns 来自数据库的响应。
@Query(value = "SELECT u FROM User u WHERE u.userID = ?")
User getUserById(String id);
我在其他项目中以这种方式创建了查询,但无法弄清楚为什么我在这个项目中会收到以下错误
JDBC style parameters (?) are not supported for JPA queries.
你试过这个吗:
@Query(value = "SELECT u FROM User u WHERE u.userID = :id")
User getUserById(String id);
Legacy-style query parameters (`?`) are no longer supported;
这是因为 hibernate 不提供对 placeholders (?) 的支持,您需要将代码更改为以下使用 named parameters
处于休眠状态。
有效。
@Query(value = "SELECT u FROM User u WHERE u.userID = :inputUserId")
User getUserById(String inputUserId);
:inputUserId // named parameter
以下是避免占位符(?).
的原因
Deprecate the Hibernate-specific (JDBC-style) positional parameters in
favor of the JPA-style.
在 HQL 中遇到同样的问题,在 XML 方法中使用 positional Parameter
异常: Legacy-style 不再支持查询参数 (?
)
HQL 查询
String hql = "from Customer cust where cust.city=?";
解决方法:修改hql查询如下
String hql = "from Customer cust where cust.city=?0";
我们开始吧!
String hql = "from Customer cust where cust.city= :city";
Query<Customer> query = session.createQuery(hql,Customer.Class);
query.setParameter("city",id);//here id is your value
我已经介绍了 2 个关于如何在休眠中编写参数化查询的示例。
package demo;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import demo.entity.Student;
public class UpdateDemo {
public static void main(String[] args) {
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Student.class)
.buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// query student table
List<Student> theStudents = session.createQuery("from Student").getResultList();
// Example1: Query with parameter
String sql = "from Student s where s.firstName=?0 and s.lastName=?1";
theStudents = session.createQuery(sql).setParameter(0, "Norman").setParameter(1, "Brandon").list();
displayResult(theStudents);
//Example 2: Query with Parameter
String sql1 = "from Student s where s.firstName=:fname and s.lastName=:lname";
Query<Student> query = session.createQuery(sql1);
query.setParameter("fname", "John");
query.setParameter("lname", "Doe");
theStudents = query.list();
displayResult(theStudents);
// commit transaction
session.getTransaction().commit();
} finally {
factory.close();
}
}
private static void displayResult(List<Student> theStudents) {
for (Student tempStudent : theStudents) {
System.out.println(tempStudent);
}
}
}
也可以在查询后加nativeQuery = true
@Query(value = "SELECT u FROM User u WHERE u.userID = ?", nativeQuery
= true)
User getUserById(String id);
您可以使用 ?1
因为您在 getUserById() 中有一个输入参数:
@Query(value = "SELECT u FROM User u WHERE u.userID = ?1")
User getUserById(String id);
在尝试为我的应用程序创建一个 API 时,我尝试进行一个查询,该查询接受一个传入的值,然后 returns 来自数据库的响应。
@Query(value = "SELECT u FROM User u WHERE u.userID = ?")
User getUserById(String id);
我在其他项目中以这种方式创建了查询,但无法弄清楚为什么我在这个项目中会收到以下错误
JDBC style parameters (?) are not supported for JPA queries.
你试过这个吗:
@Query(value = "SELECT u FROM User u WHERE u.userID = :id")
User getUserById(String id);
Legacy-style query parameters (`?`) are no longer supported;
这是因为 hibernate 不提供对 placeholders (?) 的支持,您需要将代码更改为以下使用 named parameters
处于休眠状态。
有效。
@Query(value = "SELECT u FROM User u WHERE u.userID = :inputUserId")
User getUserById(String inputUserId);
:inputUserId // named parameter
以下是避免占位符(?).
的原因Deprecate the Hibernate-specific (JDBC-style) positional parameters in favor of the JPA-style.
在 HQL 中遇到同样的问题,在 XML 方法中使用 positional Parameter
异常: Legacy-style 不再支持查询参数 (?
)
HQL 查询
String hql = "from Customer cust where cust.city=?";
解决方法:修改hql查询如下
String hql = "from Customer cust where cust.city=?0";
我们开始吧!
String hql = "from Customer cust where cust.city= :city";
Query<Customer> query = session.createQuery(hql,Customer.Class);
query.setParameter("city",id);//here id is your value
我已经介绍了 2 个关于如何在休眠中编写参数化查询的示例。
package demo;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import demo.entity.Student;
public class UpdateDemo {
public static void main(String[] args) {
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Student.class)
.buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// query student table
List<Student> theStudents = session.createQuery("from Student").getResultList();
// Example1: Query with parameter
String sql = "from Student s where s.firstName=?0 and s.lastName=?1";
theStudents = session.createQuery(sql).setParameter(0, "Norman").setParameter(1, "Brandon").list();
displayResult(theStudents);
//Example 2: Query with Parameter
String sql1 = "from Student s where s.firstName=:fname and s.lastName=:lname";
Query<Student> query = session.createQuery(sql1);
query.setParameter("fname", "John");
query.setParameter("lname", "Doe");
theStudents = query.list();
displayResult(theStudents);
// commit transaction
session.getTransaction().commit();
} finally {
factory.close();
}
}
private static void displayResult(List<Student> theStudents) {
for (Student tempStudent : theStudents) {
System.out.println(tempStudent);
}
}
}
也可以在查询后加nativeQuery = true
@Query(value = "SELECT u FROM User u WHERE u.userID = ?", nativeQuery = true)
User getUserById(String id);
您可以使用 ?1
因为您在 getUserById() 中有一个输入参数:
@Query(value = "SELECT u FROM User u WHERE u.userID = ?1")
User getUserById(String id);