use of com.querydsl.core.QueryException in project querydsl by querydsl.
the class AbstractHibernateQuery method fetchCount.
@Override
public long fetchCount() {
QueryModifiers modifiers = getMetadata().getModifiers();
try {
Query query = createQuery(modifiers, true);
Long rv = (Long) query.uniqueResult();
if (rv != null) {
return rv;
} else {
throw new QueryException("Query returned null");
}
} finally {
reset();
}
}
use of com.querydsl.core.QueryException in project querydsl by querydsl.
the class GenericExporter method innerExport.
@SuppressWarnings("unchecked")
private void innerExport() {
typeMappings = codegenModule.get(TypeMappings.class);
queryTypeFactory = codegenModule.get(QueryTypeFactory.class);
typeFactory = new TypeFactory(Arrays.asList(entityAnnotation, supertypeAnnotation, embeddableAnnotation), codegenModule.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
// copy annotations helpers to typeFactory
for (AnnotationHelper helper : annotationHelpers) {
typeFactory.addAnnotationHelper(helper);
}
// process supertypes
for (Class<?> cl : superTypes.keySet()) {
createEntityType(cl, superTypes);
}
// process embeddables
for (Class<?> cl : embeddableTypes.keySet()) {
createEntityType(cl, embeddableTypes);
}
// process entities
for (Class<?> cl : entityTypes.keySet()) {
createEntityType(cl, entityTypes);
}
// process projections
for (Class<?> cl : projectionTypes.keySet()) {
createEntityType(cl, projectionTypes);
}
// add constructors and properties
for (Map<Class<?>, EntityType> entries : Arrays.asList(superTypes, embeddableTypes, entityTypes, projectionTypes)) {
for (Map.Entry<Class<?>, EntityType> entry : new HashSet<>(entries.entrySet())) {
addConstructors(entry.getKey(), entry.getValue());
addProperties(entry.getKey(), entry.getValue());
}
}
// merge supertype fields into subtypes
Set<EntityType> handled = new HashSet<EntityType>();
for (EntityType type : superTypes.values()) {
addSupertypeFields(type, allTypes, handled);
}
for (EntityType type : entityTypes.values()) {
addSupertypeFields(type, allTypes, handled);
}
for (EntityType type : embeddableTypes.values()) {
addSupertypeFields(type, allTypes, handled);
}
// extend types
typeFactory.extendTypes();
try {
Serializer supertypeSerializer, entitySerializer, embeddableSerializer, projectionSerializer;
if (serializerClass != null) {
Serializer serializer = codegenModule.get(serializerClass);
supertypeSerializer = serializer;
entitySerializer = serializer;
embeddableSerializer = serializer;
projectionSerializer = serializer;
} else {
supertypeSerializer = codegenModule.get(SupertypeSerializer.class);
entitySerializer = codegenModule.get(EntitySerializer.class);
embeddableSerializer = codegenModule.get(EmbeddableSerializer.class);
projectionSerializer = codegenModule.get(ProjectionSerializer.class);
}
// serialize super types
serialize(supertypeSerializer, superTypes);
// serialize entity types
serialize(entitySerializer, entityTypes);
// serialize embeddable types
serialize(embeddableSerializer, embeddableTypes);
// serialize projection types
serialize(projectionSerializer, projectionTypes);
} catch (IOException e) {
throw new QueryException(e);
}
}
use of com.querydsl.core.QueryException in project querydsl by querydsl.
the class AbstractLuceneQuery method innerCount.
private long innerCount() {
try {
final int maxDoc = searcher.maxDoc();
if (maxDoc == 0) {
return 0;
}
TotalHitCountCollector collector = new TotalHitCountCollector();
searcher.search(createQuery(), getFilter(), collector);
return collector.getTotalHits();
} catch (IOException | IllegalArgumentException e) {
throw new QueryException(e);
}
}
use of com.querydsl.core.QueryException 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);
}
}
use of com.querydsl.core.QueryException in project querydsl by querydsl.
the class AbstractLuceneQuery method iterate.
@Override
public CloseableIterator<T> iterate() {
final QueryMetadata metadata = queryMixin.getMetadata();
final List<OrderSpecifier<?>> orderBys = metadata.getOrderBy();
final Integer queryLimit = metadata.getModifiers().getLimitAsInteger();
final Integer queryOffset = metadata.getModifiers().getOffsetAsInteger();
Sort sort = querySort;
int limit;
final int offset = queryOffset != null ? queryOffset : 0;
try {
limit = maxDoc();
if (limit == 0) {
return new EmptyCloseableIterator<T>();
}
} catch (IOException | IllegalArgumentException e) {
throw new QueryException(e);
}
if (queryLimit != null && queryLimit < limit) {
limit = queryLimit;
}
if (sort == null && !orderBys.isEmpty()) {
sort = serializer.toSort(orderBys);
}
try {
ScoreDoc[] scoreDocs;
int sumOfLimitAndOffset = limit + offset;
if (sumOfLimitAndOffset < 1) {
throw new QueryException("The given limit (" + limit + ") and offset (" + offset + ") cause an integer overflow.");
}
if (sort != null) {
scoreDocs = searcher.search(createQuery(), getFilter(), sumOfLimitAndOffset, sort).scoreDocs;
} else {
scoreDocs = searcher.search(createQuery(), getFilter(), sumOfLimitAndOffset).scoreDocs;
}
if (offset < scoreDocs.length) {
return new ResultIterator<T>(scoreDocs, offset, searcher, fieldSelector, transformer);
}
return new EmptyCloseableIterator<T>();
} catch (final IOException e) {
throw new QueryException(e);
}
}
Aggregations