从数据库保存和检索复选框字段的状态
我们的应用程序是基于网络的。我们的交易工具是:Java、JDK1.6、Apache Tomcat v6.0.10、PostgreSQL v8.2.3。
我们的应用程序中有一个页面/屏幕,大约有。 20 - 30 个复选框,仅表示字段的是/否状态。
我的问题是,就在我确定自己的设计/架构之前,我想与社区讨论我的实现,并获得其他人如何看待/处理此类设计注意事项的意见/建议。
我的解决方案/设计:创建一个包含 3 列的表 (USERPREFERENCE):EMPLOYEEID、FIELDID、STATE。通过唯一 ID/常量 (1, 2, 3, ..., 30) 定义/指定每个复选框字段。因此,该表中的每个用户将有 20 - 30 个条目。这是正常的还是有任何不同/更好的方法可以在数据库级别有效地处理这个问题?另外,如何将 20 - 30 个字段(按行存储)的状态自动从数据库映射到 JavaBean 对象,以便在 Java 级别轻松处理来回操作?
USERPREFERENCE 表如下所示:
EMPLOYEEID | FIELDID | STATE
100 | 1 | true
100 | 2 | true
100 | 3 | false
... | ... | ...
欢迎并赞赏任何建议/设计替代方案。
注意:我们还将开发另一个具有相同功能的 4 页面/屏幕,每个屏幕中有 20 - 30 个复选框字段。
更新:请考虑到这一点:这些字段不是固定的,将来可能会添加新字段。
Our application is web-based. Our tools of trade are: Java, JDK1.6, Apache Tomcat v6.0.10, PostgreSQL v8.2.3.
We've a page/screen in the application that has got approx. 20 - 30 checkboxes, which just only denotes yes/no state of the fields.
My question here is, just before I settle down myself on my own design/architecture, I wanted to bounce my implementation with the community and get opinion/advice on how others are looking at/handling this type of design considerations.
My solution/design: Creating a table (USERPREFERENCE) with 3 columns: EMPLOYEEID, FIELDID, STATE. Define/designate each checkbox field by a Unique ID/constant (1, 2, 3, ..., 30). Hence, there would be 20 - 30 entries for each user in this table. Is this normal or is there any different/better way of handling this effectively at the database-level? Also, how do I map the state of 20 - 30 fields (stored row-wise) from database to a JavaBean object automatically, so that it becomes ease in handling back-and-forth at Java level?
USERPREFERENCE table looks like:
EMPLOYEEID | FIELDID | STATE
100 | 1 | true
100 | 2 | true
100 | 3 | false
... | ... | ...
Any advice/design alternatives are welcome and appreciated.
NOTE: We would also be developing another 4 page/screen with the same functionality having 20 - 30 checkbox fields in each screen.
UPDATE: Please take this into account: The fields are not fixed, new fields may get added in future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果字段具有固定长度,并且大多数实体的每个字段都有一个值 (
EMPLOYEEID
),我会为每个字段选择单独的boolean
列。从两个角度来看它更高效:(EMPLOYEEID, FIELDID)
键(PostgreSQL 在存储布尔值方面很智能)If the fields are of a fixed length and you have a value for each field for most of your entities (
EMPLOYEEID
) I would opt for separateboolean
columns for each field. It's more efficient from two points of view:(EMPLOYEEID, FIELDID)
key for each value (and PostgreSQL is smart about storing booleans)