Lets say I have something like this in my database (abstract)
MyTable {
Long ID Primary Key NOT NULL,
Date ValidFrom Primary Key NOT NULL,
Date ValidTo
}
Basically an Entity with Composite primary key on ID and ValidFrom. Now I have some date range for example 2012-09-01 -> 2012-09-30.
My question is: can I made smart hibernate query, which on repeated ID, but different ValidFrom key, return this one with latest date?
For example data:
Row 1 {
Long ID = 1,
Date ValidFrom 2012-09-01,
Date ValidTo 2012-09-15
}
Row2 {
Long ID = 2,
Date ValidFrom 2012-09-01,
Date ValidTo 2012-09-15
}
Row3 {
Long ID = 1,
Date ValidFrom 2012-09-16,
Date ValidTo null
}
Selecting those rows which are between given date (2012-09-01 -> 2012-09-30) would return only Row2 and Row3 (as Row1 and Row3 havee same ID, so we get latest).
Is there any query(criteria API?) or do I need to take all entries and then filter with some method?
I ended it with making request for are Rows and then taking latest with
With Date comparator