JavaDB/Hibernate/Swing 自动排序

发布于 2024-12-16 11:23:09 字数 485 浏览 9 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-12-23 11:23:09

定义您自己的类型

class QuestionID implements Comparable<QuestionID> {…}

并在 TableModel 中使用它而不是 String。让它的构造函数提取从数据库返回的 questionID 的数字部分,并让它的 compareTo() 实现仅检查该数字。 Value 类是 JTableTest就是一个例子。

Define your own type

class QuestionID implements Comparable<QuestionID> {…}

and use it in your TableModel instead of String. Let its constructor extract the numeric part of the questionID returned from the database, and let its compareTo() implementation examine just that number. The Value class is JTableTest is an example.

暖阳 2024-12-23 11:23:09

两种想法:(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.

帅气称霸 2024-12-23 11:23:09

显然,数据库存储记录的顺序不一定与插入记录的顺序相同。因此,现在由于我必须按照将记录输入数据库的顺序来获取记录,因此我在插入时使用自动递增计数器,并在获取记录时使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文