小编kum*_*uda的帖子

使用where子句在hibernate中选择查询

我有课程登录,有userId,usernamepassword.

对于用户登录我检查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() != …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate

5
推荐指数
2
解决办法
5万
查看次数

标签 统计

hibernate ×1

java ×1

mysql ×1