没有ID字段的Grails域类或部分NULL复合字段 [英] Grails Domain Class without ID field or with partially NULL composite field

查看:151
本文介绍了没有ID字段的Grails域类或部分NULL复合字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据上一个问题的答案(在此回答: Grails中的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

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

B: >
id,A.id,C.id

1,2,1

2,2,2

3, 3,1

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

C:

id,varY

1,Boom />
2,Fizzle

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

我的视图看起来像这样:_
A.id,varX, B.id,C.id,varY

1,Blah,NULL,NULL,NULL

2,Foo,1,1,Boom br />
2,Foo,2,2,Fizzle,
3,Bar,3,1,Boom,

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"

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

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).

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

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

还要注意,我们要显示表A中的元素至少一次(在表B / C中找不到任何字段的空值),如果表B中有多个相应条目,可能需要多次。

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.

所以,我的问题是2部分:

1:是否可以定义一个没有任何ID字段的grails域类?我们不需要任何视图条目的永久句柄,我们只需要列出该视图中的数据。

2:如果不是,是否可以使用复合体定义一个grails域类ID字段,其中一部分可能为NULL,但即使组合ID的一部分为NULL,也可以唯一标识行?

我知道我们可以使用直接的Groovy SQL直接查询视图,而无需关联的域类(我们现在正在这样做),但理想情况下,我们希望用域类来表示视图。另外,除了这两个问题之外,这两个问题可以比一般问题更为普遍。

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.

推荐答案

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

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

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

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.

对于数据库视图,我们通常在其中一个连接的表中已经有一个ID,在该上下文中是唯一的,但是如果我们不这样做,那么有办法来攻击/模拟这个。例如,在SQL中,只需将您将在复合键中使用的键连接为单个字段,并将其用作键。

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.

这篇关于没有ID字段的Grails域类或部分NULL复合字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆