I have a Java swing application with Hibernate and network JavaDB/DerbyDB. There is a table which is like this in structure:
TestID QuestionID
T1234 Q1
T1234 Q2
T1234 Q3
T1234 Q4
..
..
T1234 Q10
where combination of TestID and QuestionID has been defined as the primary key. Whenever i add/insert a row with Test ID ‘T1234’ and QuestionID ‘Q10’, the record gets added immediately after the record containing QuestionID ‘Q1’. Example:
TestID QuestionID
T1234 Q1
T1234 Q10
T1234 Q3
T1234 Q4
I don’t want this to happen as this is causing me trouble while fetching the records. Could you please let me know what should i do to avoid this.
Help will be appreciated.
Apparently, databases store records in an order which is not necessarily the same as the order in which they were inserted. Hence, now since I have to fetch records in the same order as I have entered them into the database, i make use of auto incrementing counter while making the insert and use the order by clause (on the counter) while fetching the records.