I have the following problem: I’m loading a “dif”-table with only deletes and inserts. A “change” is determined by a delete followed by an insert.
This table has no primary key and no field to order by. I want to use Hibernate to load this table. (SELECT obj FROM MyDifTable obj WHERE obj.group = groupId)
Right now, I’m using a key over all values and the above query and I’m not getting the rows in the same order as in the source table.
My questions are:
- How do I let Hibernate always return the same order as in the table?
- How to specify a primary key in this case?
I know this is such a badly designed thing and I’ve tried to argument with the design team but without success.
Best Regards,
Kai
Relational databases are based upon a branch of Set theory. Information is either in the database (in the Set) or absent. Order is not a concept in sets, only presence is a concept in sets. This means that you must model ordering explicitly if you need it.
If Order must be maintained, you must add an “order by” clause to sort the resulting members of the set. Sometimes that sort is external (so you can delay decision of the ordering until query submission time). On very rare occasions it is important to have that order internalized in the “data set”.
On the odd case you need an internalized ordering, you must do so by storing the order along with the data in the set; but, to enforce the presence of the “order by” clause (query writers might omit it), you might also have to put a few stored procedures / views of the data to ensure that the query always returns with the correct ordering.
Examples of internalized ordering are common when storing a list to a database. One typically stores the list item along with it’s index in the list in the same row. That way when it is retrieved, you can restore the items and the ordering.