我需要创建新的简单实体以查询而不加入JOOQ?
我有一些看起来像这样的实体:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPA实体是对数据进行建模的一种方式,类似于
在SQL中创建表
。如果您合理地将事物归一化,则实际上,无论是以SQL的形式和JPA实体的形式,您都没有太多选择。但是,出于所有错误的原因,人们也一直在 project 这些实体,即使使用JPA,您也可以像JOOQ一样投影任意数据结构。您的
数据类
不是实体,是预测。不是同一回事。像在SQL中一样,您可以在其中编写数据的任意视图,而SQL不会告诉您这样做或这样做,Jooq对您的 Projections 没有任何意见。因此,过程是:
进行设计)。最佳查询,因此,您(团队)的偏好,数据表示,要求等自然而然地出现了对您个人需求的最佳预测。只要您可以在合理的情况下代表事物,就没有对或错误的投影方式。
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:
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.