我需要创建新的简单实体以查询而不加入JOOQ?

发布于 2025-01-29 03:41:06 字数 1189 浏览 2 评论 0原文

我有一些看起来像这样的实体:

data class ComplexEntity(
   private val id: UUID,
   private val srcSomethingId: UUID,
   private val dstSomethingId: UUID,
   private val creationDate: LocalDateTime,
   private val updatingDate: LocalDateTime?,
   private val creationAuthorId: UUID,
   private val updatingAuthorId: UUID?,
   private val roles: List<UUID>,
   private val fieldsId: UUID,
   // relation
   private val srcSomething: SomethingEntity?,
   private val dstSomething: SomethingEntity?
)

它有关系,并且我将其与另一个表一起加入,但是我大部分时间都不需要关系,所以也许我需要创建更简单的实体,例如投影或保持关系字段null不需要时。

data class ComplexProjection (
   private val id: UUID,
   private val srcSomethingId: UUID,
   private val dstSomethingId: UUID,
   private val creationDate: LocalDateTime,
   private val updatingDate: LocalDateTime?,
   private val creationAuthorId: UUID,
   private val updatingAuthorId: UUID?,
   private val roles: List<UUID>,
   private val fieldsId: UUID
)

或VICE VESA停留复杂的预测像主实体一样,因为它大部分时间都需要并将其称为复杂性 ,并在其中pot stut Respanders complectentitywithryations 。

最好的方法是什么?

实际上

I have some entity that look like that:

data class ComplexEntity(
   private val id: UUID,
   private val srcSomethingId: UUID,
   private val dstSomethingId: UUID,
   private val creationDate: LocalDateTime,
   private val updatingDate: LocalDateTime?,
   private val creationAuthorId: UUID,
   private val updatingAuthorId: UUID?,
   private val roles: List<UUID>,
   private val fieldsId: UUID,
   // relation
   private val srcSomething: SomethingEntity?,
   private val dstSomething: SomethingEntity?
)

It has relation and i'm joining it with another table, but I don't need the relation most of the time, so maybe I need to create more simplified entity like projection or just stay relation fields null when they are not needed.

data class ComplexProjection (
   private val id: UUID,
   private val srcSomethingId: UUID,
   private val dstSomethingId: UUID,
   private val creationDate: LocalDateTime,
   private val updatingDate: LocalDateTime?,
   private val creationAuthorId: UUID,
   private val updatingAuthorId: UUID?,
   private val roles: List<UUID>,
   private val fieldsId: UUID
)

Or vice versa stay ComplexProjection like main entity because it need most of the time and call it ComplexEntity and make ComplexEntityWithRelations where put relations.

What is the best way to do it?

P.S. Actually, I don't need all of this fields most of the times, but I think additional 5 field don't hurt performance so much.

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

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

发布评论

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

评论(1

旧街凉风 2025-02-05 03:41:06

JPA实体是对数据进行建模的一种方式,类似于在SQL中创建表。如果您合理地将事物归一化,则实际上,无论是以SQL的形式和JPA实体的形式,您都没有太多选择。

但是,出于所有错误的原因,人们也一直在 project 这些实体,即使使用JPA,您也可以像JOOQ一样投影任意数据结构。您的数据类不是实体,是预测。不是同一回事。像在SQL中一样,您可以在其中编写数据的任意视图,而SQL不会告诉您这样做或这样做,Jooq对您的 Projections 没有任何意见。

因此,过程是:

  1. 将您的架构标准化到足够的水平(例如3NF)
  2. 根据您的需求设计您的查询(例如,UI需要从表A中的这3列和从表B中的这2个嵌套集合中

进行设计)。最佳查询,因此,您(团队)的偏好,数据表示,要求等自然而然地出现了对您个人需求的最佳预测。只要您可以在合理的情况下代表事物,就没有对或错误的投影方式。

JPA entities are a way of modelling your data, similar to CREATE TABLE in SQL. If you normalise things reasonably, indeed, you don't have too many options, both in the form of SQL and in the form of JPA entities.

But, for all the wrong reasons, people also project those entities all the time, even if with JPA, you can project arbitrary data structures, just like with jOOQ. Your data classes aren't entities, they're projections. It's not the same thing. Like in SQL, where you can write arbitrary views for your data, and SQL wouldn't tell you to do it this or that way, jOOQ has no opinion about your projections.

So, the process is this:

  1. Normalise your schema to a sufficient level (e.g. 3NF)
  2. Design your queries according to your needs (e.g. the UI needs these 3 columns from table A and these 2 nested collections from table B)

That's it. The best query and thus the best projection for your individual needs arise naturally, from your (team's) preferences, your data representation, requirements, etc. There's no right or wrong projection of your data, as long as you can represent things in a reasonable way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文