user2838630
user2838630

Reputation: 81

How to set placeholders in hibernate

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

Upvotes: 1

Views: 2520

Answers (6)

Atul Singh Rathore
Atul Singh Rathore

Reputation: 35

Sorry This Is not Answer but instead another case, in above case @Grigoriev Nick suggested

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

but here the script starts with directly from clause while what if I want want to used sql like below

  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

Upvotes: 0

Peyman Charchian
Peyman Charchian

Reputation: 1

Try this one.

   Session ses = HibernateUtil.getSessionFactory().openSession();
      String query = "from RegisterPojo where email = '" + varEmailAddress + "' and pwd = '" + varPassword + "'";
      List<RegisterPojo> list = ses.createQuery(query).list();
  ses.close();

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122006

You have to modify your query like this,

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

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

Read the hql docs here

Upvotes: 1

Todoy
Todoy

Reputation: 1266

You should use a prepared statement instead of a string. example here

  PreparedStatement preparedStatement = con.prepareStatement  ("from RegisterPojo where email=? and pwd=?");
preparedStatement.setString(1, "email");
preparedStatement.setString(2, "password");

Upvotes: 1

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26084

Try this one.

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

Upvotes: 3

Grigoriev Nick
Grigoriev Nick

Reputation: 1129

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

Upvotes: 0

Related Questions