在 ORMLite 中,当 Object 似乎可以工作时,为什么我们应该使用具有特定类型 ID 的 DAO?

发布于 2025-01-13 19:30:53 字数 440 浏览 3 评论 0原文

例如,我可以使用 DaoDao 来扩展 BaseDaoImpl。当 Object 似乎也能正常工作时,为什么我要指定 UUID?

到目前为止,在我的大型项目中的所有 Dao 实现中使用 Object 一直有效。我以为我可能遇到了启用后破坏对象缓存功能的情况,但我对 ORMLite 的 ReferenceObjectCache.java 中用于存储和检索 Map 中的引用的模式进行的测试表明,它对于强类型键都可以正常工作或键转换为对象。

我仍然没有弄清楚为什么对象缓存功能在启用它后对我不起作用(相同的数据,不同的对象),但是试图弄清楚这一点让我想知道为什么甚至有理由在 a 中指定 ID 类型首先是 ORMLite DAO。

I can extend BaseDaoImpl using either Dao<InvoiceItem, Object> or Dao<InvoiceItem, UUID>, for instance. Why would I specify UUID when Object seems to work just as well?

Using Object in all of my Dao implementations in a large-ish project have been working so far. I thought I may have tripped over a case where it was breaking object cache functionality after enabling it, but my testing of the pattern used in ORMLite's ReferenceObjectCache.java to store and retrieve references from a Map showed that it works fine with either strongly typed keys or keys cast as Object.

I still haven't figured out why the object cache feature is not working for me after enabling it (same data, different objects), but trying to figure this out has me wondering why there is even a reason to specify the ID type in a ORMLite DAO to begin with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2025-01-20 19:30:53

例如,我可以使用 DaoDao 来扩展 BaseDaoImpl。当 Object 似乎也能正常工作时,为什么我要指定 UUID

Dao 是一个泛型类。当您谈论 ListList相比时,您的问题会类似。泛型类型意味着当您调用 dao.deleteById(uuid) 时,它将提供类型检查以验证 id 是否是正确的类。如果您使用 Object id,则可能会不恰当地调用 dao.deleteById("hello"),如果 ID 字段是 UUID,则不会找到您的对象

此外,如果您调用 UUID id = dao.extractId(data),则不必强制转换结果,因为编译器知道该方法返回 UUID

到目前为止,在我的大型项目中的所有 Dao 实现中使用 Object 一直有效。

泛型类型实际上仅适用于调用者,并提供编译时类型检查。在底层,BaseDaoImpl 中的ID 类型只是一个对象。

这是 Oracle 的教程,介绍为什么我们使用泛型帮助你的理解。他们的主要原因是:

  • 编译时更强的类型检查。
  • 消除演员阵容。
  • 程序员可以实现适用于不同类型集合的算法。

I can extend BaseDaoImpl using either Dao<InvoiceItem, Object> or Dao<InvoiceItem, UUID>, for instance. Why would I specify UUID when Object seems to work just as well?

Dao is a generic class. Your question would be similar when you are talking about List<UUID> compared to List<Object>. The generic types mean that when you call dao.deleteById(uuid) it would provide type checking to validate that the id was the correct class. If you were using an Object id, you could inappropriately call dao.deleteById("hello") which would not find your object if the ID field was a UUID.

Also, if you call UUID id = dao.extractId(data), you don't have to cast the result because the compiler knows that the method returns a UUID.

Using Object in all of my Dao implementations in a large-ish project have been working so far.

The generic types are really only for the caller and provides compiler time type checking. Under the covers the ID type in the BaseDaoImpl is just an object.

Here's the tutorial from Oracle on why we use generics that might help your understanding. Their top reasons are:

  • Stronger type checks at compile time.
  • Elimination of casts.
  • Programmers can implement algorithms that work on collections of different types.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文