JavaDB/Hibernate/Swing 自动排序
我有一个带有 Hibernate 和网络 JavaDB/DerbyDB 的 Java swing 应用程序。有一个表,结构如下:
TestID QuestionID
T1234 Q1
T1234 Q2
T1234 Q3
T1234 Q4
..
..
T1234 Q10
其中 TestID 和 QuestionID 的组合已被定义为主键。每当我添加/插入带有测试 ID 'T1234' 和 QuestionID 'Q10' 的行时,记录就会立即添加到包含 QuestionID 'Q1' 的记录之后。示例:
TestID QuestionID
T1234 Q1
T1234 Q10
T1234 Q3
T1234 Q4
我不希望发生这种情况,因为这在获取记录时给我带来了麻烦。您能否让我知道我该怎么做才能避免这种情况。
帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
定义您自己的类型
并在
TableModel
中使用它而不是String
。让它的构造函数提取从数据库返回的questionID
的数字部分,并让它的compareTo()
实现仅检查该数字。Value
类是JTableTest
就是一个例子。Define your own type
and use it in your
TableModel
instead ofString
. Let its constructor extract the numeric part of thequestionID
returned from the database, and let itscompareTo()
implementation examine just that number. TheValue
class isJTableTest
is an example.两种想法:(a) 将 QuestionID 存储为 1、10、3、4 等,并在应用程序中添加“Q”前缀,或者 (b) 使用 0 填充存储 QuestionID,如:Q001、Q010、Q003 、Q004 等
Two ideas: (a) store QuestionID as 1, 10, 3, 4, etc., and add the 'Q' prefix in your application, or (b) store QuestionID with 0-padding, as in: Q001, Q010, Q003, Q004, etc.
显然,数据库存储记录的顺序不一定与插入记录的顺序相同。因此,现在由于我必须按照将记录输入数据库的顺序来获取记录,因此我在插入时使用自动递增计数器,并在获取记录时使用 order by 子句(在计数器上)。
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.