使用 isEmpty() 检查 JDO 查询是否安全?什么是“正确的”?方式?
根据文档此处(在“唯一结果”下)和这里,一个JDO“默认”查询结果将返回一个List,和一个“ unique”指定的查询如果满足则返回 null是空的。
那么我可以安全地使用 .isEmpty()
来检查我是否获得了正常查询的行吗?同样,对于查询被标记为“唯一”的情况,我是否应该使用 == null
?
我测试过,空结果确实返回一个 .size()
为 0
的列表。所以看来 NullPointerException 的唯一危险在于“唯一”查询。
我还应该采取哪些其他预防措施(如果有)?我只用这两种类型的支票就能满足我的基本要求吗?几乎正在寻找一些最佳实践建议以及任何潜在的问题。我对 Java 和 JDO/ORM 持久性都很陌生。
谢谢
According to the documentation here (under "unique results") and here, A JDO "default" query result will return a List, and a "unique" specified query returns null if they are empty.
So am I safe to just use .isEmpty()
to check that I got rows for a normal query? Likewise for those times that a query is marked "unique", should I just use == null
?
I tested and a empty result does indeed return a List with .size()
of 0
. So it seems my only danger of a NullPointerException lies with a "unique" query.
What other precautions should I take (if any)? Do I cover my bases acceptably with just those two types of checks? Pretty much looking for some best practice advice, and any potential gotcha's. I am pretty novice with both Java and JDO/ORM persistence.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法?列表就是一个具有 Java 契约 (API) 的列表。 JDO 并没有改变这一点。 JDO 规范(和 DN 文档)还定义了查询的结果形式
http://www.datanucleus.org/products/accessplatform_3_0/jdo/jdoql_result.html
Right way ? A List is a List and that has a Java contract (API). JDO doesn't change that. Also the JDO spec (and DN docs) define what form of result comes from a query
http://www.datanucleus.org/products/accessplatform_3_0/jdo/jdoql_result.html