Java 类org.hibernate.NonUniqueResultException 实例源码

项目:Hotel-Properties-Management-System    文件:RoomDaoImpl.java   
@Override
public Room getRoomByRoomNumber(String roomNumber) {

    try {

        session = dataSourceFactory.getSessionFactory().openSession();
        beginTransactionIfAllowed(session);
        Query<Room> query = session.createQuery("from Room where number=:roomNumber", Room.class);
        query.setParameter("roomNumber", roomNumber);

        logging.setMessage("RoomDaoImpl -> fetching room by number "+roomNumber);
        return query.getSingleResult();

    } catch (NonUniqueResultException e) {
        logging.setMessage("RoomDaoImpl -> "+e.getLocalizedMessage());
        final InformationFrame frame = new InformationFrame();
        frame.setMessage("There is more than one room with this number!");
        frame.setVisible(true);
    }
    session.close();
    return null;
}
项目:Hotel-Properties-Management-System    文件:RoomDaoImpl.java   
@Override
public Room getRoomByReservId(long id) {

    try {
        session = dataSourceFactory.getSessionFactory().openSession();
        beginTransactionIfAllowed(session);
        Query<Room> query = session.createQuery("from Room where ReservationId=:id", Room.class);
        query.setParameter("id", id);
        logging.setMessage("RoomDaoImpl -> fetching room by identity :"+id);
        return query.getSingleResult();

    } catch (NonUniqueResultException e) {
        session.getTransaction().rollback();
        logging.setMessage("RoomDaoImpl -> "+e.getLocalizedMessage());
        final InformationFrame frame = new InformationFrame();
        frame.setMessage(e.getLocalizedMessage());
        frame.setVisible(true);
    }
    session.close();
    return null;
}
项目:unitimes    文件:DepartmentalInstructor.java   
public static DepartmentalInstructor findByPuidDepartmentId(String puid, Long deptId, org.hibernate.Session hibSession) {
    try {
    return (DepartmentalInstructor)hibSession.
        createQuery("select d from DepartmentalInstructor d where d.externalUniqueId=:puid and d.department.uniqueId=:deptId").
        setString("puid", puid).
        setLong("deptId",deptId.longValue()).
        setCacheable(true).
        setFlushMode(FlushMode.MANUAL).
        uniqueResult();
    } catch (NonUniqueResultException e) {
        Debug.warning("There are two or more instructors with puid "+puid+" for department "+deptId+" -- returning the first one.");
        return (DepartmentalInstructor)hibSession.
            createQuery("select d from DepartmentalInstructor d where d.externalUniqueId=:puid and d.department.uniqueId=:deptId").
            setString("puid", puid).
            setLong("deptId",deptId.longValue()).
            setCacheable(true).
            setFlushMode(FlushMode.MANUAL).
            list().get(0);
    }
}
项目:unison    文件:HibernateHelper.java   
/**
 * Find by key.
 *
 * @param key
 *            the key
 * @param session
 *            the session
 * @param objclass
 *            the objclass
 * @return the object
 * @throws HibernateException
 *             the hibernate exception @+ aram objclass
 */
public Object findByKey(final String key, final Session session, final Class<?> objclass)
        throws HibernateException {

    final String queryText = objclass.getName() + ".findByKey";
    final Query query = session.getNamedQuery(queryText);
    query.setString("key", key);
    Object uniqueResult = null;
    try {
        uniqueResult = query.uniqueResult();

    }
    catch (final NonUniqueResultException e) {
        throw new RuntimeException(
                "Got non-unique result for " + key + " on " + objclass.getName() + " " + e);
    }
    return uniqueResult;
}
项目:eMonocot    文件:UserServiceImpl.java   
/**
 *
 * @param username Set the username
 * @param newPassword Set the new password
 */
@Transactional(readOnly = false)
public final void changePasswordForUser(final String username,
        final String newPassword) {
    Assert.hasText(username);
    Assert.hasText(newPassword);

    try {
        User user = dao.find(username);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }

        Object salt = this.saltSource.getSalt(user);

        String password = passwordEncoder.encodePassword(newPassword, salt);
        ((User) user).setPassword(password);

        dao.update((User) user);
        userCache.removeUserFromCache(user.getUsername());
    } catch (NonUniqueResultException nure) {
        throw new IncorrectResultSizeDataAccessException(
                "More than one user found with name '" + username + "'", 1);
    }
}
项目:eMonocot    文件:UserServiceImpl.java   
/**
 * DO NOT CALL THIS METHOD IN LONG RUNNING SESSIONS OR CONVERSATIONS A
 * THROWN UsernameNotFoundException WILL RENDER THE CONVERSATION UNUSABLE.
 *
 * @param username
 *            Set the username
 * @return the userdetails of the user
 */
@Transactional(readOnly = true)
public final UserDetails loadUserByUsername(final String username) {
 try {
     Assert.hasText(username);
 } catch (IllegalArgumentException iae) {
     throw new UsernameNotFoundException(username, iae);
 }
 try {
     User user = dao.load(username);
     userCache.putUserInCache(user);
     return user;
 } catch (ObjectRetrievalFailureException orfe) {
     throw new UsernameNotFoundException(username, orfe);
 } catch (NonUniqueResultException nure) {
     throw new IncorrectResultSizeDataAccessException(
             "More than one user found with name '" + username + "'", 1);
 }
}
项目:unitime    文件:DepartmentalInstructor.java   
public static DepartmentalInstructor findByPuidDepartmentId(String puid, Long deptId, org.hibernate.Session hibSession) {
    try {
    return (DepartmentalInstructor)hibSession.
        createQuery("select d from DepartmentalInstructor d where d.externalUniqueId=:puid and d.department.uniqueId=:deptId").
        setString("puid", puid).
        setLong("deptId",deptId.longValue()).
        setCacheable(true).
        setFlushMode(FlushMode.MANUAL).
        uniqueResult();
    } catch (NonUniqueResultException e) {
        Debug.warning("There are two or more instructors with puid "+puid+" for department "+deptId+" -- returning the first one.");
        return (DepartmentalInstructor)hibSession.
            createQuery("select d from DepartmentalInstructor d where d.externalUniqueId=:puid and d.department.uniqueId=:deptId").
            setString("puid", puid).
            setLong("deptId",deptId.longValue()).
            setCacheable(true).
            setFlushMode(FlushMode.MANUAL).
            list().get(0);
    }
}
项目:karaku    文件:BaseDAOImpl.java   
@Override
public T getByExample(EntityExample<T> example) {

    Where<T> where = new Where<T>();
    where.setExample(example);
    ISearchParam isp = new SearchParam();
    isp.setLimit(2);
    isp.setOffset(0);

    List<T> result = this.getAll(where, isp);
    if (!ListHelper.hasElements(result)) {
        return null;
    }
    if (result.size() == 1) {
        return result.get(0);
    }

    throw new NonUniqueResultException(result.size());

}
项目:ns2gather    文件:GatherPlayersManager.java   
private void completeInfo(final PlayerDTO playerDTO) throws LogicException, ClientAuthException {
    HibernateUtil.exec(new HibernateCallback<Void>() {

        @Override
        public Void run(Session session) throws LogicException, ClientAuthException {
            try {
                Player playerEnt = (Player) session.createQuery("from Player p where p.steamId = :sid")
                        .setLong("sid", playerDTO.getId()).uniqueResult();
                if (playerEnt != null) {
                    playerDTO.setNick(playerEnt.getNick());
                }
            } catch (NonUniqueResultException e) {
                throw LogicExceptionFormatted.format("Дубликация информации об игроке %d", playerDTO.getId());
            }
            return null;
        }
    });

}
项目:AmadeusLMS    文件:CourseHibernateDAO.java   
public boolean courseNameExist(Course course) {
    boolean retorno = false;

    try {
        Criteria crit = getSession().createCriteria(getPersistentClass());
        crit.add( eq("name", course.getName()).ignoreCase() );

        Course c = (Course) crit.uniqueResult();

        if(c != null && c.getId() != course.getId()) {
            retorno = true;
        }
    } catch (NonUniqueResultException e) {
        retorno = true;
    }

    return retorno;
}
项目:gwtutil    文件:NestedSetManagerNG.java   
/**
 * Retrieves parent node by child node id.
 * 
 * @param childId
 * @param depth
 *            negative values mean parent by Math.abs(depth) levels above,
 *            non-negative values mean absolute depth.
 * @return parent node
 * @throws LogicException
 * @throws ClientAuthException
 */
@SuppressWarnings("unchecked")
public T getParentByChild(final Long childId, final Long depth) throws LogicException, ClientAuthException {
    T childNode = (T) session.get(entityClass, childId);
    Long parentDepth = null;
    if (depth < 0) {
        parentDepth = childNode.getDepth() + depth;
        if (parentDepth < 0) {
            throw new NestedSetManagerException(
                    "parentDepth < 0, childDepth = " + childNode.getDepth() + " requested depth = " + depth);
        }
    } else {
        parentDepth = depth;
    }
    try {
        return (T) session
                .createQuery("from " + entityName + " node where node.leftnum <= :left and node.rightnum >= :right and depth = :depth")
                .setLong("left", childNode.getLeftNum()).setLong("right", childNode.getRightNum()).setLong("depth", parentDepth)
                .uniqueResult();
    } catch (NonUniqueResultException e) {
        throw new NestedSetManagerException("Non-unique parent: " + e.getMessage());
    }
}
项目:gwtutil    文件:NestedSetManagerTS.java   
/**
 * Retrieves parent node by child node id.
 * 
 * @param childId
 * @param depth
 *            negative values mean parent by Math.abs(depth) levels above,
 *            non-negative values mean absolute depth.
 * @return parent node
 * @throws LogicException
 * @throws ClientAuthException
 */
@SuppressWarnings("unchecked")
public T getParentByChild(final Long childId, final Long depth) throws LogicException, ClientAuthException {
    T childNode = (T) session.get(entityClass, childId);
    Long parentDepth = null;
    if (depth < 0) {
        parentDepth = childNode.getDepth() + depth;
        if (parentDepth < 0) {
            throw new NestedSetManagerException(
                    "parentDepth < 0, childDepth = " + childNode.getDepth() + " requested depth = " + depth);
        }
    } else {
        parentDepth = depth;
    }
    try {
        return (T) session
                .createQuery("from " + entityName + " node where node.leftnum <= :left and node.rightnum >= :right and depth = :depth")
                .setLong("left", childNode.getLeftNum()).setLong("right", childNode.getRightNum()).setLong("depth", parentDepth)
                .uniqueResult();
    } catch (NonUniqueResultException e) {
        throw new NestedSetManagerException("Non-unique parent: " + e.getMessage());
    }
}
项目:spring-data-snowdrop    文件:JpaDatasourceMapper.java   
protected T single() {
    try {
        //noinspection unchecked
        return (T) fullTextQuery.uniqueResult();
    } catch (NonUniqueResultException ex) {
        throw new IncorrectResultSizeDataAccessException(ex.getMessage(), 1);
    } finally {
        close();
    }
}
项目:lams    文件:AbstractQueryImpl.java   
static Object uniqueElement(List list) throws NonUniqueResultException {
    int size = list.size();
    if (size==0) return null;
    Object first = list.get(0);
    for ( int i=1; i<size; i++ ) {
        if ( list.get(i)!=first ) {
            throw new NonUniqueResultException( list.size() );
        }
    }
    return first;
}
项目:communote-server    文件:TaskDaoImpl.java   
/**
 * {@inheritDoc}
 * 
 * @see com.communote.server.persistence.tasks.TaskDao#findTaskByUniqueName()
 */
@Override
protected Task handleFindTaskByUniqueName(String uniqueName) {
    List<?> result = getHibernateTemplate().find(QUERY_FIND_BY_UNIQUE_NAME, uniqueName);
    if (result.size() > 1) {
        throw new NonUniqueResultException(result.size());
    }
    return result.size() == 1 ? (Task) result.get(0) : null;
}
项目:cacheonix-core    文件:AbstractQueryImpl.java   
static Object uniqueElement(List list) throws NonUniqueResultException {
    int size = list.size();
    if (size==0) return null;
    Object first = list.get(0);
    for ( int i=1; i<size; i++ ) {
        if ( list.get(i)!=first ) {
            throw new NonUniqueResultException( list.size() );
        }
    }
    return first;
}
项目:Lucee4    文件:HibernateORMSession.java   
private Object uniqueResult(org.hibernate.Query query) throws PageException {
    try{
        return query.uniqueResult();
    }
    catch(NonUniqueResultException e){
        List list = query.list();
        if(list.size()>0) return list.iterator().next();
        throw CommonUtil.toPageException(e);
    }
    catch(Throwable t){
        lucee.commons.lang.ExceptionUtil.rethrowIfNecessary(t);
        throw CommonUtil.toPageException(t);
    }
}
项目:gwtutil    文件:TemporalManager.java   
@SuppressWarnings("unchecked")
public synchronized void save(T element) throws LogicException {
    try {
        T priorObject = (T) makeCriteria(element.getUniqValue()).add(Restrictions.eq(START_DATE, element.getStartDate()))
                .uniqueResult();
        if (priorObject != null) {
            session.delete(priorObject);
        }
        session.saveOrUpdate(element);
    } catch (NonUniqueResultException e) {
        throw LogicExceptionFormatted.format("Non-unique result when searching for objects at " + DATE_FORMAT, element.getStartDate());
    }
}
项目:gwtutil    文件:NestedSetManagerNG.java   
@SuppressWarnings("unchecked")
public T getRootNode() throws LogicException, ClientAuthException {
    try {
        return (T) session.createQuery("from " + entityName + " where leftnum = 1").uniqueResult();
    } catch (NonUniqueResultException e) {
        throw new LogicException("Duplicate root entries, DB is corrupted.");
    }
}
项目:gwtutil    文件:NestedSetManagerTS.java   
@SuppressWarnings("unchecked")
public T getRootNode() throws LogicException, ClientAuthException {
    try {
        return (T) session.createQuery("from " + entityName + " where leftnum = 1").uniqueResult();
    } catch (NonUniqueResultException e) {
        throw new LogicException("Duplicate root entries, DB is corrupted.");
    }
}
项目:gwtutil    文件:NestedSetManager.java   
/**
 * Retrieves parent node by child node id.
 * 
 * @param childId
 * @param depth
 *            negative values mean parent by Math.abs(depth) levels above, non-negative values mean absolute depth.
 * @return parent node
 * @throws LogicException
 * @throws ClientAuthException
 */
public synchronized T getParentByChild(final Long childId, final Long depth) throws GwtUtilException {
    return HibernateUtil.exec(new HibernateCallback<T>() {

        @SuppressWarnings("unchecked")
        @Override
        public T run(Session session) throws GwtUtilException {
            T childNode = (T) session.get(entityClass, childId);
            Long parentDepth = null;
            if (depth < 0) {
                parentDepth = childNode.getDepth() + depth;
                if (parentDepth < 0) {
                    throw new NestedSetManagerException(
                            "parentDepth < 0, childDepth = " + childNode.getDepth() + " requested depth = " + depth);
                }
            } else {
                parentDepth = depth;
            }
            try {
                return (T) session
                        .createQuery("from " + entityName
                                + " node where node.leftnum <= :left and node.rightnum >= :right and depth = :depth")
                        .setLong("left", childNode.getLeftNum()).setLong("right", childNode.getRightNum()).setLong("depth", parentDepth)
                        .uniqueResult();
            } catch (NonUniqueResultException e) {
                throw new NestedSetManagerException("Non-unique parent: " + e.getMessage());
            }
        }
    });
}
项目:gwtutil    文件:NestedSetManager.java   
public synchronized T getRootNode() throws GwtUtilException {
    return HibernateUtil.exec(new HibernateCallback<T>() {

        @SuppressWarnings("unchecked")
        @Override
        public T run(Session session) throws GwtUtilException {
            try {
                return (T) session.createQuery("from " + entityName + " where leftnum = 1").uniqueResult();
            } catch (NonUniqueResultException e) {
                throw new LogicException("Duplicate root entries, DB is corrupted.");
            }
        }
    });
}
项目:zeprs    文件:PersistenceManager.java   
/**
 * Gets all instances of this object this patient id and pregnancy
 *
 * @param patientId
 * @return the object
 * @throws PersistenceException If an error occurs in the persistence layer
 */
public Identifiable getPatientData(Long patientId, Long pregnancyId) throws PersistenceException, ObjectNotFoundException, NonUniqueResultException;