English 中文(简体)
基于起始日期和结束日期的综合关键
原标题:GORM composite key based on start and end dates

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

这三个方面将形成一个主要的关键会议。 我要补充几点限制,以确保日期不会重叠等。

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

标 题 B号为A,2011-11-03年,然后在2011-11-07年检索物体A。

最佳回答

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.

问题回答

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

如果你处理与同一目标有多个日期范围的情况,那么你就应当安排数据,以代表母物体和代表日期范围清单的儿童物体。

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

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

铁路公司确实很简单,可以删除铁路,因此没有真正的理由不这样做。





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签