基于开始和结束日期的 GORM 复合键

发布于 2024-12-13 13:04:08 字数 456 浏览 2 评论 0原文

我想要一个表,其中我想要有按日期范围排列的条目。 我通常是如何做的,就是有一张桌子: someId [不唯一] 开始日期 endDate

这三个将创建一个主键+我会添加一些约束以确保日期不重叠等。

从数据库的角度来看一切都很好,但是当我想在 Grails 中创建一个域类来处理它时..嗯,这比我想象的要困难。 有没有办法确保我是否有关系: ClassA 有一个 ClassB [ClassB 将按日期排列] 我在 ClassB 中有条目:

Id  StartDate  EndDate     Name
1   2011-11-01 2011-11-05  A
1   2011-11-06 2011-11-10  B

并在 2011-11-03 将名称为 A 的 objectB 分配给 objectA,然后在 2011-11-07 检索 objectA 它将指向名称为 B 的 objectB?

I want to have a table, where I want to have entries, which are date ranged.
How I normally did it, was to have a table with:
someId [not unique]
startDate
endDate

These three would create a primary key + I would add few constraints to make sure dates don't overlap etc.

Everything is fine from the database point of view, however when I want to create a domain class in Grails to handle it... well it's more difficult then I thought.
Is there a way, to make sure that if I have a relation:
ClassA hasOne ClassB [ClassB would be date ranged]
and I have entries in ClassB:

Id  StartDate  EndDate     Name
1   2011-11-01 2011-11-05  A
1   2011-11-06 2011-11-10  B

and assign objectB with name A to objectA on 2011-11-03 and then retrieve objectA on 2011-11-07 it will point to objectB with name B?

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

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

发布评论

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

评论(2

执手闯天涯 2024-12-20 13:04:08

这似乎是设计数据库的糟糕方法。在大多数情况下,您确实应该为对象使用单个数字 ID。

如果您正在处理有多个日期范围与同一对象关联的情况,那么您应该通过具有父对象和表示日期范围列表的子对象来构建数据来表示该情况。

class Foo {
    static hasMany = [bars:Bar]
    static mapping = {
        bars lazy:false
    }
    ...
}

class Bar {
    static belongsTo = [foo:Foo]
    Date startDate
    Date endDate
    ...
}

Grails 使级联删除变得非常简单,因此没有真正的理由不以这种方式创建它。

That seems like a poor way to design your database. In most cases, you really should just use a single numeric ID for objects.

If you are dealing with a situation where you have multiple date ranges associated with the same object, then you should structure your data to represent that, by having a parent object, and a child object that represents the list of date ranges.

class Foo {
    static hasMany = [bars:Bar]
    static mapping = {
        bars lazy:false
    }
    ...
}

class Bar {
    static belongsTo = [foo:Foo]
    Date startDate
    Date endDate
    ...
}

Grails makes it really simple to have cascade deletes, so there's no real reason not to create it this way.

萌酱 2024-12-20 13:04:08

我们最终做的是:
在三列上创建索引和约束[在数据库上,因为在 Grails 1.3.7 中创建索引被破坏并且约束处理很疯狂],创建了静态 startDateendDate 变量其中保存当前范围的开始日期和结束日期。这样我们就能够维持我们想要的功能并且它的性能非常好。

What we did in the end was:
create index and constraint on the three columns [on the database, since creating indexes in Grails 1.3.7 is broken and constraint handling is insane], created static startDate and endDate variables which hold start and end date of the current range. This way we were able to maintain the functionality we wanted and it performs really well.

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