【问题标题】:Unable to pass positional parameters into HQL query无法将位置参数传递到 HQL 查询
【发布时间】:2018-09-26 15:57:43
【问题描述】:

在尝试为我的应用程序创建 API 时,我尝试进行查询,该查询接受传入的值,然后从数据库返回响应。

@Query(value = "SELECT u FROM User u WHERE u.userID = ?")
User getUserById(String id);

我在其他项目中以这种方式创建了查询,但无法弄清楚为什么我在这个项目中收到以下错误

JDBC style parameters (?) are not supported for JPA queries.

【问题讨论】:

    标签: java hibernate hql


    【解决方案1】:

    你试过了吗:

    @Query(value = "SELECT u FROM User u WHERE u.userID  = :id")
    User getUserById(String id);
    

    【讨论】:

    【解决方案2】:

    在 HQL 中遇到同样的问题,在 XML 方法中使用 positional 参数

    例外:不再支持旧式查询参数 (?)

    HQL 查询

    String hql = "from Customer cust where cust.city=?";
    

    解决方法:如下更改hql查询

    String hql = "from Customer cust where cust.city=?0";
    

    我们来了!!

    【讨论】:

      【解决方案3】:
      Legacy-style query parameters (`?`) are no longer supported;
      

      这是因为 hibernate 不支持 placeholders (?) ,您需要将代码更改为在 hibernate 中使用 named parameters 的以下代码。

      有效。

      @Query(value = "SELECT u FROM User u WHERE u.userID = :inputUserId")
      User getUserById(String inputUserId);
      
      :inputUserId // named parameter
      

      以下是避免使用占位符 (?) 的原因。

      弃用 Hibernate 特定(JDBC 样式)的位置参数 支持 JPA 风格。

      【讨论】:

        【解决方案4】:
        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
        

        【讨论】:

          【解决方案5】:

          我已经介绍了 2 个关于如何在 hibernate 中编写参数化查询的示例。

          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);
                  }
              }
          
          }
          

          【讨论】:

            【解决方案6】:

            您也可以在查询后添加nativeQuery = true

            @Query(value = "SELECT u FROM User u WHERE u.userID = ?", nativeQuery =真)

            用户 getUserById(String id);

            【讨论】:

              【解决方案7】:

              您可以使用?1,因为您在 getUserById() 中有一个输入参数:

              @Query(value = "SELECT u FROM User u WHERE u.userID = ?1")
              User getUserById(String id);
              

              【讨论】:

                猜你喜欢
                • 2015-08-26
                • 2023-03-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多