java 当 Hibernate 可能为空时,如何从 Hibernate 返回唯一结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33926068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a unique result from Hibernate when it may be null?
提问by mad_fox
What is the cleanest wayto return a Hibernate unique result when it might be null
?
返回 Hibernate 唯一结果的最简洁方法是null
什么?
Is there anything wrong with this solution:
这个解决方案有什么问题吗:
public Category getCategoryById(int id) {
Object result = currentSession.createCriteria(Category.class)...uniqueResult();
return (Category) result;
}
And is there a better way to do this?
有没有更好的方法来做到这一点?
采纳答案by Tobias Liefke
There is no clean wayto do this, as it depends on your API.
没有干净的方法可以做到这一点,因为这取决于您的 API。
If you express that your method could return null
, especially in the JavaDoc - possibly supported by a @Nullable
, nothing is wrong to return null
here.
如果您表示您的方法可以 return null
,尤其是在 JavaDoc 中 - 可能由 a 支持,那么在这里@Nullable
返回没有错null
。
I usually do that, if I expect that the requested value does not exist in some validstate in my application:
我通常会这样做,如果我希望我的应用程序中的某些有效状态不存在请求的值:
/**
* Tries to find a category by its id.
*
* @param id the id
* @return the category for that id or {@code null} if not found
*/
@Nullable
public Category findCategoryById(int id) {
Object result = ....uniqueResult();
return (Category) result;
}
On the other hand you can throw an exception if an missing element would be invalid and document that as well:
另一方面,如果缺少的元素无效,您可以抛出异常并记录该异常:
/**
* Resolve a category by its id.
*
* @param id the id as given by another method
* @return the category for that id
* @throws NoSuchElementException if the element does not exist
*/
@Nonnull
public Category getCategoryById(int id) {
Object result = ....uniqueResult();
if (result == null) {
throw new NoSuchElementException("category for id: " + id);
}
return (Category) result;
}
(I have to admit that I'm using the annotations only on occasions)
(我不得不承认我只是偶尔使用注释)
I'm using different method names (findCategoryById
v.s. getCategoryById
) for both situations. If you stick to a naming scheme, the users of your API will know what to expect without reading the JavaDoc.
我在两种情况下都使用不同的方法名称(findCategoryById
vs getCategoryById
)。如果您坚持命名方案,您的 API 用户将无需阅读 JavaDoc 就知道会发生什么。
In Java 8 and Google Guava there is a combination of both solutions: Optional
在 Java 8 和 Google Guava 中,有两种解决方案的组合: Optional
/**
* Finds a category by its id.
*
* @param id the id
* @return the category for that id, an empty value if not found
*/
public Optional<Category> findCategoryById(int id) {
Object result = ....uniqueResult();
return Optional.ofNullable((Category) result);
}
The advantage here is, that the caller can decide if he expects the value to exist or not:
这里的优点是,调用者可以决定他是否希望该值存在:
// The value must exist, otherwise a NoSuchElementException is thrown:
...findCategoryById(id).get();
// The value could be null:
...findCategoryById(id).orElse(defaultValue);
The biggest problem is, that many Java developers are not used to it up to now, but I guess that will improve in time...
最大的问题是,很多 Java 开发人员到现在还不习惯,但我想这会随着时间的推移而改进......
Additional reading material
附加阅读材料
There is a community wiki as well for some (or more) points about the caller side of the when to check for null problem: Avoiding != null statements
关于何时检查空问题的调用方的一些(或更多)点,还有一个社区维基:避免 != null 语句