Search in sources :

Example 1 with NonUniqueResultException

use of com.querydsl.core.NonUniqueResultException in project querydsl by querydsl.

the class AbstractLuceneQuery method oneResult.

@Nullable
private T oneResult(boolean unique) {
    try {
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), getFilter(), maxDoc).scoreDocs;
        int index = 0;
        QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
        Long offset = modifiers.getOffset();
        if (offset != null) {
            index = offset.intValue();
        }
        Long limit = modifiers.getLimit();
        if (unique && (limit == null ? scoreDocs.length - index > 1 : limit > 1 && scoreDocs.length > 1)) {
            throw new NonUniqueResultException("Unique result requested, but " + scoreDocs.length + " found.");
        } else if (scoreDocs.length > index) {
            Document document;
            if (fieldSelector != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldSelector);
            } else {
                document = searcher.doc(scoreDocs[index].doc);
            }
            return transformer.apply(document);
        } else {
            return null;
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) QueryModifiers(com.querydsl.core.QueryModifiers) QueryException(com.querydsl.core.QueryException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with NonUniqueResultException

use of com.querydsl.core.NonUniqueResultException in project querydsl by querydsl.

the class AbstractLuceneQuery method oneResult.

@Nullable
private T oneResult(boolean unique) {
    try {
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), getFilter(), maxDoc, Sort.INDEXORDER, false, false).scoreDocs;
        int index = 0;
        QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
        Long offset = modifiers.getOffset();
        if (offset != null) {
            index = offset.intValue();
        }
        Long limit = modifiers.getLimit();
        if (unique && (limit == null ? scoreDocs.length - index > 1 : limit > 1 && scoreDocs.length > 1)) {
            throw new NonUniqueResultException("Unique result requested, but " + scoreDocs.length + " found.");
        } else if (scoreDocs.length > index) {
            Document document;
            if (fieldsToLoad != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldsToLoad);
            } else {
                document = searcher.doc(scoreDocs[index].doc);
            }
            return transformer.apply(document);
        } else {
            return null;
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) QueryModifiers(com.querydsl.core.QueryModifiers) QueryException(com.querydsl.core.QueryException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with NonUniqueResultException

use of com.querydsl.core.NonUniqueResultException in project querydsl by querydsl.

the class AbstractHibernateQuery method fetchOne.

@SuppressWarnings("unchecked")
@Override
public T fetchOne() throws NonUniqueResultException {
    try {
        QueryModifiers modifiers = getMetadata().getModifiers();
        Query query = createQuery(modifiers, false);
        try {
            return (T) query.uniqueResult();
        } catch (org.hibernate.NonUniqueResultException e) {
            throw new NonUniqueResultException(e);
        }
    } finally {
        reset();
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) Query(org.hibernate.query.Query) org.hibernate(org.hibernate)

Example 4 with NonUniqueResultException

use of com.querydsl.core.NonUniqueResultException in project querydsl by querydsl.

the class AbstractLuceneQuery method oneResult.

@Nullable
private T oneResult(boolean unique) {
    try {
        int maxDoc = maxDoc();
        if (maxDoc == 0) {
            return null;
        }
        final ScoreDoc[] scoreDocs = searcher.search(createQuery(), maxDoc, Sort.INDEXORDER, false, false).scoreDocs;
        int index = 0;
        QueryModifiers modifiers = queryMixin.getMetadata().getModifiers();
        Long offset = modifiers.getOffset();
        if (offset != null) {
            index = offset.intValue();
        }
        Long limit = modifiers.getLimit();
        if (unique && (limit == null ? scoreDocs.length - index > 1 : limit > 1 && scoreDocs.length > 1)) {
            throw new NonUniqueResultException("Unique result requested, but " + scoreDocs.length + " found.");
        } else if (scoreDocs.length > index) {
            Document document;
            if (fieldsToLoad != null) {
                document = searcher.doc(scoreDocs[index].doc, fieldsToLoad);
            } else {
                document = searcher.doc(scoreDocs[index].doc);
            }
            return transformer.apply(document);
        } else {
            return null;
        }
    } catch (IOException | IllegalArgumentException e) {
        throw new QueryException(e);
    }
}
Also used : NonUniqueResultException(com.querydsl.core.NonUniqueResultException) QueryModifiers(com.querydsl.core.QueryModifiers) QueryException(com.querydsl.core.QueryException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NonUniqueResultException (com.querydsl.core.NonUniqueResultException)4 QueryException (com.querydsl.core.QueryException)3 QueryModifiers (com.querydsl.core.QueryModifiers)3 IOException (java.io.IOException)3 Document (org.apache.lucene.document.Document)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 Nullable (org.jetbrains.annotations.Nullable)3 org.hibernate (org.hibernate)1 Query (org.hibernate.query.Query)1