Java 使用 where 子句在休眠中选择查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27034359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:42:02  来源:igfitidea点击:

select query in hibernate with where clause

javamysqlhibernate

提问by kumuda

I have class Login which has userId, usernameand password.

我有类 Login ,它有userId,usernamepassword

For a user to login I am checking usernameand passwordand geting userId. If userIdis not zero then it will lead to home page. I am trying to do this in hibernate. But my query is not working

对于用户登录我检查username,并password和歌厅userId。如果userId不是零,那么它将导致主页。我正在尝试在休眠状态下执行此操作。但我的查询不起作用

public int userLogin(Login login)
        throws MySQLIntegrityConstraintViolationException, SQLException,
        Exception {

    Session session = HibernateUtil.getSessionFactory().openSession();
    int userId = 0;

    try {
            session.beginTransaction();
            String hql = "Select log.userId from Login log where log.userName=:userName 
            and log.password=:password";      
            System.out.println(hql);
            Query query = session.createQuery(hql);
            query.setParameter(":userName", login.getUserName());
            query.setParameter(":password", login.getPassword());
            List result = query.list();

            System.out.println("resultset:"+result);

            Iterator iterator = result.iterator();
            while(iterator.hasNext()){
                userId = (int) iterator.next();


        }
    } catch (HibernateException e) {
        if (session.getTransaction() != null) {
            session.getTransaction().rollback();
        }
    } finally {
        session.close();
    }

采纳答案by Shoaib Chikate

1) You are using HQL, so you need to understand that you can't give column names which are in database in projections of HQL query

1)您使用的是HQL,因此您需要了解您不能在HQL查询的投影中给出数据库中的列名

 String hql = "select user_id from login where user_name= :username and  
            password= :password";

Here in your Login class you don't have field as user_idand you gave user_idinto projections. HQL maps class with database, hence Login class will login table and userId field will be user_id column in database.And what you wrote is plain SQL query not HQL query.

在您的 Login 类中,您没有字段 asuser_id并且您进行user_id了预测。HQL 将类与数据库映射,因此登录类将登录表,userId 字段将是数据库中的 user_id 列。您编写的是普通 SQL 查询而不是 HQL 查询。

Please use this HQL query.

请使用此 HQL 查询。

String hql="Select log.userId from Login log where log.username=:username and log.password=:password"

Here log is alias name like we do in plain Java.

这里 log 是别名,就像我们在普通 Java 中所做的一样。

Login log=new Login()
log.userId

回答by kamalakerreddy k

Try not use colon while setting the param.

设置参数时尽量不要使用冒号。

query.setParameter("userName", login.getUserName());