Skip to content

Commit

Permalink
Explicitly declare NonUniqueResultException for fetchOne
Browse files Browse the repository at this point in the history
Fetchable.fetchOne() declares in it's javadoc that it throws
NonUniqueResultException. It should therefore be explicitly declared in
the method signature as well.
All implementations must also declare in their signature that they throw.

This also has the desirable side-effect of the javadoc of all of the
implementors inheriting the @throws section from the javadoc of the
interface as well.

Fixes #2232
  • Loading branch information
Zoltán Reegn committed Jan 16, 2018
1 parent 7afba09 commit 3a74aae
Show file tree
Hide file tree
Showing 18 changed files with 27 additions and 23 deletions.
Expand Up @@ -196,7 +196,7 @@ public QueryResults<T> fetchResults() {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
queryMixin.setUnique(true);
if (queryMixin.getMetadata().getModifiers().getLimit() == null) {
limit(2L);
Expand Down
Expand Up @@ -46,7 +46,7 @@ public interface Fetchable<T> {
* @throws NonUniqueResultException if there is more than one matching result
* @return first result or null
*/
T fetchOne();
T fetchOne() throws NonUniqueResultException;

/**
* Get the projection as a typed closeable Iterator
Expand Down
Expand Up @@ -37,7 +37,7 @@ public T fetchFirst() {

@Nullable
@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
if (results.size() > 1) {
throw new NonUniqueResultException();
} else if (results.isEmpty()) {
Expand Down
Expand Up @@ -152,11 +152,11 @@ public T fetchFirst() {

@SuppressWarnings("unchecked")
@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
try {
return (T) createQuery(false).uniqueResult();
} catch (org.hibernate.NonUniqueResultException e) {
throw new NonUniqueResultException();
throw new NonUniqueResultException(e);
}
}

Expand Down
Expand Up @@ -307,7 +307,7 @@ public String toString() {

@Nullable
@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
if (getMetadata().getModifiers().getLimit() == null) {
limit(2);
}
Expand Down
Expand Up @@ -222,7 +222,7 @@ public String toString() {
@SuppressWarnings("unchecked")
@Override
@Nullable
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
if (getMetadata().getModifiers().getLimit() == null) {
limit(2);
}
Expand Down
3 changes: 2 additions & 1 deletion querydsl-jpa/src/main/java/com/querydsl/jpa/JPASubQuery.java
Expand Up @@ -15,6 +15,7 @@

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
Expand Down Expand Up @@ -62,7 +63,7 @@ public JPASubQuery<Tuple> select(Expression<?>... exprs) {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
throw new UnsupportedOperationException();
}

Expand Down
Expand Up @@ -322,14 +322,14 @@ public Q setTimeout(int timeout) {

@SuppressWarnings("unchecked")
@Override
public T fetchOne() {
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();
throw new NonUniqueResultException(e);
}
} finally {
reset();
Expand Down
Expand Up @@ -211,7 +211,7 @@ protected void reset() {

@SuppressWarnings("unchecked")
@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
try {
Query query = createQuery();
return (T) uniqueResult(query);
Expand All @@ -225,7 +225,7 @@ private Object uniqueResult(Query query) {
try {
return query.uniqueResult();
} catch (org.hibernate.NonUniqueResultException e) {
throw new NonUniqueResultException();
throw new NonUniqueResultException(e);
}
}

Expand Down
Expand Up @@ -247,15 +247,15 @@ protected void reset() {
@Nullable
@SuppressWarnings("unchecked")
@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
try {
Query query = createQuery(getMetadata().getModifiers(), false);
return (T) getSingleResult(query);
} catch (javax.persistence.NoResultException e) {
logger.trace(e.getMessage(),e);
return null;
} catch (javax.persistence.NonUniqueResultException e) {
throw new NonUniqueResultException();
throw new NonUniqueResultException(e);
} finally {
reset();
}
Expand Down
Expand Up @@ -292,7 +292,7 @@ protected void reset() {

@Override
@SuppressWarnings("unchecked")
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
Query query = createQuery();
return (T) uniqueResult(query);
}
Expand All @@ -305,7 +305,7 @@ private Object uniqueResult(Query query) {
logger.trace(e.getMessage(),e);
return null;
} catch (javax.persistence.NonUniqueResultException e) {
throw new NonUniqueResultException();
throw new NonUniqueResultException(e);
} finally {
reset();
}
Expand Down
3 changes: 2 additions & 1 deletion querydsl-jpa/src/test/java/com/querydsl/jpa/QueryHelper.java
Expand Up @@ -26,6 +26,7 @@

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void parse() throws RecognitionException, TokenStreamException {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
throw new UnsupportedOperationException();
}

Expand Down
Expand Up @@ -330,7 +330,7 @@ public T fetchFirst() {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
return oneResult(true);
}

Expand Down
Expand Up @@ -332,7 +332,7 @@ public T fetchFirst() {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
return oneResult(true);
}

Expand Down
Expand Up @@ -372,7 +372,7 @@ public T fetchFirst() {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
return oneResult(true);
}

Expand Down
Expand Up @@ -338,7 +338,7 @@ public K fetchOne(Path<?>... paths) {
}

@Override
public K fetchOne() {
public K fetchOne() throws NonUniqueResultException {
try {
Long limit = queryMixin.getMetadata().getModifiers().getLimit();
if (limit == null) {
Expand Down
Expand Up @@ -25,6 +25,7 @@
import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.FetchableQuery;
import com.querydsl.core.JoinFlag;
import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.Query;
import com.querydsl.core.QueryFlag;
import com.querydsl.core.QueryFlag.Position;
Expand Down Expand Up @@ -391,7 +392,7 @@ public <RT> Q unionAll(Path<?> alias, SubQueryExpression<RT>... sq) {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
if (getMetadata().getModifiers().getLimit() == null
&& !queryMixin.getMetadata().getProjection().toString().contains("count(")) {
limit(2);
Expand Down
3 changes: 2 additions & 1 deletion querydsl-sql/src/main/java/com/querydsl/sql/UnionImpl.java
Expand Up @@ -18,6 +18,7 @@
import javax.annotation.Nullable;

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.Query;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.QueryResults;
Expand Down Expand Up @@ -55,7 +56,7 @@ public T fetchFirst() {
}

@Override
public T fetchOne() {
public T fetchOne() throws NonUniqueResultException {
return query.fetchOne();
}

Expand Down

0 comments on commit 3a74aae

Please sign in to comment.