没有 ID 字段或具有部分 NULL 复合字段的 Grails 域类

发布于 2024-07-11 17:58:49 字数 1194 浏览 3 评论 0原文

根据对上一个问题的回答(此处回答:Grails 中的 SQL/数据库视图),我尝试使用域类来表示数据库中的视图。 然而,这在大多数情况下效果很好:

我有一个没有唯一键的视图。 假设基础表如下所示:

A:
id,varX
1、“废话”
2、“福”
3、“酒吧”

B:
id,A.id,C.id
1,2,1
2,2,2
3,3,1

C:
id,varY
1、“轰”
2、“Fizzle”

我的观点是这样的:
A.id,varX,B.id,C.id,varY
1,"废话",NULL,NULL,NULL
2,"Foo",1,1,"轰隆"
2,"Foo",2,2,"嘶嘶声"
3,"Bar",3,1,"Boom"

这正是我们的目的所应该的样子。 然而,正如您所看到的,我们可以为视图构建的最佳唯一复合 id 是 ['A.id','C.id'],因为这唯一地标识了每个元素,但 Grails 失败了,因为它似乎无法处理复合 ID 的一部分为 NULL(实际上,list() 返回 4 个对象的列表,第一个是空指针,其余的是视图的实际域实例)。

请注意,我们也可以使用 A.id 和 B.id,但它会遇到同样的问题。

另请注意,我们希望至少显示一次表 A 中的元素(表 B/C 中未找到的任何字段为空值),如果表 B 中有多个相应条目,则可能显示多次。

所以,我的问题分为 2 部分:
1:是否可以定义一个根本没有任何ID字段的grails域类? 我们不需要任何视图条目的永久句柄,我们只需要列出该视图中的数据。
2:如果没有,是否可以定义一个带有复合ID字段的grails域类,其中一部分可能为NULL,但即使复合ID的一部分为NULL,它仍将唯一标识一行

?我们知道我们可以使用直接的 Groovy SQL 来直接查询视图,而无需关联的域类(我们现在实际上正在这样做),但理想情况下我们希望用域类来表示视图。 此外,抛开所有争论不谈,这两个问题的应用范围比我们的特定问题要广泛得多。

Per an answer to a previous question (answer here: SQL/Database Views in Grails), I have tried to use a domain class to represent a view in my database. This works wonderfully in most cases, however:

I have a view with no single unique key. Let's say the underlying tables look like this:

A:
id,varX
1,"Blah"
2,"Foo"
3,"Bar"

B:
id,A.id,C.id
1,2,1
2,2,2
3,3,1

C:
id,varY
1,"Boom"
2,"Fizzle"

My view looks like this:
A.id,varX,B.id,C.id,varY
1,"Blah",NULL,NULL,NULL
2,"Foo",1,1,"Boom"
2,"Foo",2,2,"Fizzle"
3,"Bar",3,1,"Boom"

That is exactly how it ought to look for our purposes. However, as you can see, the best unique composite id we could construct for the view is ['A.id','C.id'], as this uniquely identifies each element, but Grails fails because it cannot seem to deal with part of a composite ID being NULL (actually, list() returns a list of 4 objects, the first is a null pointer, the rest are actual domain instances of the view).

Note that we could also use A.id and B.id but it suffers from the same problem.

Note also that we WANT to display elements from table A at least once (with null values for any fields not found in tables B/C), possibly many times if there are multiple corresponding entries in table B.

So, my question is 2 parts:
1: Is it possible to define a grails domain class without any ID field at all? We do not need a permanent handle to any of the views entries, we just need to list the data in that view.
2: If not, is it possible to define a grails domain class with a composite ID field, part of which may be NULL, but which will uniquely identify a row anyway even if part of the composite ID is NULL?

I know we can use straight Groovy SQL to query the view directly without an associated domain class (we are doing this now actually), but ideally we'd like to represent the view with a domain class. Further, all arguments aside, these two questions can be applied much more generically than to just our particular problem.

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

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

发布评论

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

评论(2

那片花海 2024-07-18 17:58:49

我们以前也遇到过这个问题。

根据我的经验,复合 ID 在 Grails 或 Hibernate 中并没有得到很好的支持。

我们总是找到一种方法为所有域类提供唯一的 ID。

对于真实的表,我们只需添加一个自动增量字段,即使我们不使用 grails,我们也会使用复合键。

对于数据库视图,我们通常在其中一个连接表中已经有一个 ID,该 ID 在该上下文中最终是唯一的,但如果没有,有一些方法可以破解/模拟它。 例如,在 SQL 中,只需将要在复合键中使用的键作为单个字段连接起来,并将其用作键。

We had that problem before.

In my experience, Composite IDs are just not well supported in Grails or maybe in Hibernate.

We always find a way to have unique ID for all our domain classes.

For real tables, we just add an auto-increment field, even though if we weren't using grails we would have used a Composite key.

For database views, we usually have an ID already in one of the joined tables that ends up being unique in that context, but if we don't, there are ways to hack/simulate this. For example, in SQL, just concatenate the keys you would use in your composite key as a single field and use this as the key.

魔法少女 2024-07-18 17:58:49

您是否尝试过此参考页面上的复合 ID 部分提到的内容? 我自己没有尝试过使用它,我不确定这在最新版本中是否有所改变。

另外,如果您查看表 B,A.id 的唯一值是 2 和 3,所以您不认为当 A.id 为 1 时,B.id、C.id 和 varY 的结果将为空吗? 或者这只是一个例子?

Have you tried what the Composite Ids section mentions on this reference page? I have not tried using it myself and I am not sure if this has changed in the latest version.

Also, if you look at Table B, the only values for A.id are 2 and 3 so don't you think when A.id is 1 the result would be null for B.id, C.id and varY? or was that only meant as an example?

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