如何使用JTable来显示和编辑从实体、属性、值(EAV)存储区(关系DBMS)检索的实体的属性属性?
我知道这是一个有很多可能答案的问题,所以请在回答之前先看看下面的要求。
我保证投票给你的答案显示你已经读过并理解了整个事情(只要它们不是完全愚蠢的)。
用户需要能够:
通过attributes
系统需求:
实体的数量:需要扩展到entities
我看过的事物:
- Pros: - Flexible about column handling
- Easy to implement sort/filter of entities
- Flexible about column display format & editing
- Cons: - One object per entity (if objects are complex, memory overhead becomes a serious memory problem!)
- Object responsible for all functionality... but objects should be simple for memory reasons
- How do I support user-selectable columns without a HashMap for EVERY entity object?
。
- Pros:
- Paging of results avoids memory problem
- Searching/Filtering is directly in SQL
- Memory-friendly, doesn't have to make an object per-row
- Cons:
- Implementing custom columns & sorting is a pain (table header renderer, managing sort columns and order, etc)!
- Probably have to write custom JTableColumnModel too, and this gets messy!
- Has to manipulate SQL a lot, so if DB schema changes, have to rewrite multiple pieces of code!
- Hard to maintain entity ID info
- Pros:
- Designed to map DB rows to objects
- Provides object management
- Cons:
- WORST POSSIBLE solution for entity-attribute-value model
- Have to learn & write ORM code in addition to DBMS & Java code!
- Entities can have _any_ number of attributes, ORM is only good with static, limited object attributes
- Lose flexibility/speed of custom SQL
是否有更好的选择,我错过了,或者一些聪明的方法,使釉面列表或自定义表模型更容易?
我已经完全放弃了ORM作为一个选项,因为它与EAV存储有多么糟糕的匹配。
发布于 2009-06-24 17:26:38
我认为最好的选择是“使用JDBC ResultSet的表单映射数据扩展AbstractTableModel”,因为
,
,
编辑:是我要做的一件疯狂的事情,我要创建一个自定义组件,并使用位置良好的JTextField和JComboBox来呈现自己,并执行编辑操作。
Edit2:基于您的评论的。在执行消防.()调用之前,保存选定项的位置。顺便说一句,我不认为调用会重置排序或选择--这是没有问题的。
如果添加列,则只需获取新列的键字段和值。在列中显示它们。然后在后台执行隐藏的完全重新加载,并在完成后将模型交换到该模型。这实际上是在一个表中同时从多个ResultSets中运行的。
移除很容易,因为您没有显示该列的值。
Edit3:
DefaultRowSorter没那么深。它为您的记录维护一个重新索引表。因此,当JTable请求第10行时,行排序器将检查其索引表的第十个条目,并从实际模型中检索该indexth元素。
另外,如果模型中有很多相同的字符串,那么在查询数据库中的数据时,可以使用一个简单的字符串映射来缓存字符串。这样,大量多余的字符串对象就可以立即变成GC-d。
Edit4:
我会将新字段查询为键到值的映射,并让我的主模型包含一个键到值的映射列表。然后,我将使用一个getValue()实现,它根据需要从这些附加映射的主数据源返回值。我将从主模型中查找行的键,并使用它从其他映射中检索实际值。(顺便说一句。从接受的答案中获得的声誉不受每日限制。)
https://stackoverflow.com/questions/1039694
复制相似问题