【发布时间】:2016-07-13 11:52:52
【问题描述】:
在返回类型和异常处理方面,以下哪种业务方法最好?
/**
* Finds an entity identified by given arguments.
* @param ...
* @returns found entity of {@code null} if not found.
*/
public MyEntity find1(...) {
try {
return query.getSingleResult();
} catch (final NoResultException nre) {
return null;
}
}
/**
* Finds an entity identified by given arguments. Note that this method
* may raise {@code NoResultException}.
* See {@link TypedQuery#getSingleResult()}.
* @param ...
* @returns found entity
*/
public MyEntity find2(...) {
...
return query.getSingleResult(); // may throw NoResultException
}
/**
* Finds an entity identified by given arguments.
* @param ...
* @returns an optional of found entity; never {@code null}
*/
public Optional<MyEntity> find3(...) {
try {
return Optional.of(query.getSingleResult());
} catch (final NoResultException nre) {
return Optional.empty();
}
}
【问题讨论】:
-
在这种情况下,您对“最佳”的定义是什么?
-
@ThomasKilian 正确还是正确?
标签: ejb business-logic