org.hibernate.NonUniqueResultException: query did not return a unique result: 2

An exception org.hibernate.NonUniqueResultException: query did not return a unique result: 2 was thrown when the interceptor was configured . org.hibernate.NonUniqueResultException: query did not return a unique result: n is generally the case in the project. It is caused by the session.createSQLQuery(sql.toString()).uniqueResult() code. When the number of data items queried is greater than 1, the above exception will occur when the uniqueResult() method is used, so the solution is easy to solve .         First, limit the field to the only one         . Second, do not call this method if there are multiple pieces of data in the query.        Third, try{}catch{} captures and handles exceptions accordingly. After careful inspection, I found that it turned out that there were two admin users in the database, which led to two results when I searched the database, and I have the following content in UserServiceImpl.java














package com.jxust.oa.service.impl;

    import org.apache.commons.codec.digest.DigestUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import com.jxust.oa.base.BaseDaoImpl;
    import com.jxust.oa.domain.User;
    import com.jxust.oa.service.UserService;

    @Service
    public class UserServiceImpl extends BaseDaoImpl<User> implements UserService {

        public User getByIdLoginNameAndPassword(String loginName, String password) {
            return (User) getSession().createQuery(//
                    "FROM User u WHERE u.loginName=? AND u.password=?")//
                    .setParameter(0, loginName)//
                    .setParameter(1, DigestUtils.md5Hex(password))//The digest to use MD5
                    .uniqueResult();
        }

   
    }

 
  

UniqueResult() is added to createQuery, its function is to ensure that the data return value is unique, thus realizing the uniqueness to the user

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989882&siteId=291194637