Reputation: 23
i wanna transfer this code SQL to HQL i try this code but it dosn't work
public Admin connect(String login, String password)
{
SessionFactory sessionFactory =createSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Admin admin=(Admin) session.createQuery(from admin where login_admin='"+login+"' and password_admin='"+password+"');
session.getTransaction().commit();
return admin;
my class
public class Admin extends personne implements java.io.Serializable {
private String loginAdmin;
private String passwordAdmin;}
sorry for my bad english
Upvotes: 1
Views: 2917
Reputation: 63991
Use the following code:
Query query = session.createQuery("from Admin where loginAdmin= :login and password = :passwordAdmin");
query.setParameter("login", login);
query.setParameter("password", password );
Admin admin = (Admin)query.uniqueResult();
Creating the HQL query (or any other type of DB query that is) by concatenating Strings is a bad idea since that way you are opening up your system to SQL injections!
Upvotes: 2
Reputation: 1331
Hibernate will Convert your HQL to SQL.And you can write HQL query as given below to get a particular object(or record). createQuery()
expects HQL query not SQL query.
If your class is like this
class Admin{
String loginAdmin;
String passwordAdmin;
}
Change your SQL query to HQL as given below
Admin admin=(Admin) session.createQuery
(from Admin where loginAdmin='"+login+"' and passwordAdmin='"+password+"').uniqueResult();
You can refer uniqueResult() method here
Upvotes: 0
Reputation: 1310
In order to call sql queries call session.createSQLQuery(sql query here); So your code is wrong.
In order to call HQL queries call:
session.createQuery("from Admin where loginAdmin='"+login+"' and passwordAdmin='"+password+"'");
This is assuming the class is Admin with properties login and password
Upvotes: 0