1

Hi this my java code here am using hibernate to check whether this email id and password exist in db or not could anybody plz exp line me how to place the value to this place holders.

Session ses = HibernateUtil.getSessionFactory().openSession();
            String query;
            query = "from RegisterPojo where email=? and pwd=? ";
            List<RegBean> list = ses.createQuery(query).list();

            ses.close();

Thanks in advance

4

6 回答 6

3

试试这个。

 Session ses = HibernateUtil.getSessionFactory().openSession();
      String query = "from RegisterPojo rp where rp.email = :emailAddress and rp.pwd = :password";
      List<RegisterPojo> list = ses.createQuery(query)
      .setParameter("emailAddress", Your email address)
      .setParameter("password", Your password)
      .list();
      ses.close();
于 2013-10-23T12:57:31.690 回答
1

你必须像这样修改你的查询,

 query = "from RegisterPojo where email =:email and pwd =:password ";

 List<RegisterPojo> list = ses.createQuery(query)
 .setParameter("email",emailVal)
 .setParameter("password",emailVal)
 .list();

在此处阅读 hql 文档

于 2013-10-23T12:55:51.653 回答
1

您应该使用准备好的语句而不是字符串。这里的例子

  PreparedStatement preparedStatement = con.prepareStatement  ("from RegisterPojo where email=? and pwd=?");
preparedStatement.setString(1, "email");
preparedStatement.setString(2, "password");
于 2013-10-23T13:01:22.707 回答
0

试试这个。

   Session ses = HibernateUtil.getSessionFactory().openSession();
      String query = "from RegisterPojo where email = '" + varEmailAddress + "' and pwd = '" + varPassword + "'";
      List<RegisterPojo> list = ses.createQuery(query).list();
  ses.close();
于 2013-10-23T13:15:47.167 回答
0
Session ses = HibernateUtil.getSessionFactory().openSession();
            String query;
            query = "from RegisterPojo where email=? and pwd=? ";
            List<RegBean> list = ses.createQuery(query).setParameter(0,emailVal).setParameter(1,emailVal).list();

            ses.close();
于 2013-10-23T12:57:25.300 回答
0

抱歉,这不是答案,而是另一种情况,在上述情况下,@Grigoriev Nick 建议

 query = "from RegisterPojo where email=? and pwd=? ";
            List<RegBean> list = ses.createQuery(query).setParameter(0,emailVal).setParameter(1,emailVal).list();

但是这里的脚本直接子句开始,而如果我想使用下面的 sql 怎么办

  WITH A (
    /// do some selection from many tables using various union as per my requirement
  ),
   B (
   ///  another set of sqls for different set of data
   )
select XXX from A a join B b on a.XYZ = b.xyz
where 
    /// few more fixed where clause conditions
   AND A.SOME_COLUMN = ?  // Here Instead Of String Concatenation I want to 
//use Query parameters but using hibernate instead of raw Prepares statements

于 2019-09-27T08:34:47.313 回答