在 Hibernate 中映射多行每项对象
我遇到了一些,呃,非正统的设计,我不太确定如何处理它。我试图映射的表如下所示:
TABLE example {
ID INT,
CATEGORY VARCHAR,
PROPERTY VARCHAR,
VALUE VARCHAR);
单个 id 可以有几行(显然,不是主键)。例如,它可能看起来像:
# ID CATEGORY PROPERTY VALUE
1 general_info name order 1
1 general_info date 1/1/2009
...
每个 ID 可能有几个不同的类别。对于任何给定的(id、类别)组合,属性名称都是唯一的。
(编辑)ID 字段是不同表中对象的外键。我需要能够仅使用 ID 字段从这些对象获取存储在该表中的各种属性。如果复合键是可行的方法,那么我如何链接它们?
(EDIT2) 我还认为您在这里缺少的细节是第一列中具有相同 ID 的所有数据在概念上属于同一个对象。我不想为每个 (ID,CATEGORY) 组合都有一个单独的实例。
显然,这不是很正常化。最坏的情况是,我设置了一些额外的表,这些表已标准化并复制所有内容,但我想知道是否有人可以建议一种明智的方法来直接将此信息获取到休眠支持的对象中?如果需要的话,在某种字符串属性包中。
顺便说一句,我正在使用休眠注释。
I'm encountering somewhat of an, uh, unorthodox design and I'm not quite sure how to handle it. The table I'm trying to map looks like:
TABLE example {
ID INT,
CATEGORY VARCHAR,
PROPERTY VARCHAR,
VALUE VARCHAR);
A single id can have several rows (obviously, not a primary key). As an example, it could look like:
# ID CATEGORY PROPERTY VALUE
1 general_info name order 1
1 general_info date 1/1/2009
...
Every ID might have several different categories for it. Property names are unique for any given (id, category) combination.
(EDIT) The ID field is a foreign key to objects in a different table. I need to be able to get from these objects to the various properties stored in this table, using only the ID field. If a composite key is the way to go, how do I then link them?
(EDIT2) I also think the detail you're missing here is that all the data with the same ID in column one conceptually belongs to the same object. I don't want a separate instance for every (ID,CATEGORY) combination.
Obviously, this isn't very normalized. Worst case scenario, I set up some extra tables that are normalized and copy everything over, but I was wondering if anyone could suggest a sensible way to get this information into hibernate backed objects directly? If necessary in some sort of bag of String properties.
I'm using hibernate-annotations btw.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用包含 ID、CATEGORY 和 PROPERTY 的复合键。请参阅 hibernate 中的多个键如何操作? 和 JPA - 实体设计问题 有关如何实现此示例的示例(@EmbeddedId 是关键!)
Use a composite key with ID, CATEGORY and PROPERTY. See Multiple key in hibernate how to? and JPA - Entity design problem for examples on how to implements this (@EmbeddedId is the key!)
由于 ID 是另一个表(我们将该表称为“容器”)的外键,因此可以将其映射为具有组合键的 Map。
示例:
映射:
Since the ID is a foreign key to another table (let's call that table 'container'), this can be mapped as a Map with a composite key.
Example:
Mapping: