在 Hibernate 中映射多行每项对象

发布于 2024-09-15 23:44:53 字数 711 浏览 11 评论 0原文

我遇到了一些,呃,非正统的设计,我不太确定如何处理它。我试图映射的表如下所示:

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 技术交流群。

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

发布评论

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

评论(2

一抹淡然 2024-09-22 23:44:53

使用包含 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!)

翻了热茶 2024-09-22 23:44:53

由于 ID 是另一个表(我们将该表称为“容器”)的外键,因此可以将其映射为具有组合键的 Map。

示例:

public class Container {
    private int id;
    private Map<Key,String> values = new HashMap<Key,String>();

    public String getValue(String category, String property) {
        return values.get(new Key(category, property));
    }

    public void setValue(String category, String property, String value) {
        values.put(new Key(category, property), value);
    }

    public static class Key {
        private String category;
        private String property;

        public Key(String cat, String prop) {
            category = cat;
            property = prop;
        }

        public String getCategory() {
            return category;
        }

        public String getProperty() {
            return property;
        }

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Key)) {
                return false;
            }
            Key k = (Key)obj;
            return category.equals(k.category) && property.equals(k.property);
        }

        @Override
        public int hashCode() {
            return 37*category.hashCode() + property.hashCode();
        }
    }
}

映射:

<class name="Container" table="container">
    <id column="ID" name="id">
        <generator class="native"/>
    </id>
    <map cascade="all-delete-orphan" name="values" table="example">
        <key column="ID"/>
        <composite-map-key class="Container$Key">
            <key-property name="category" type="string" column="CATEGORY"/>
            <key-property name="property" type="string" column="PROPERTY"/>
        </composite-map-key>
        <element type="string" column="VALUE"/>
    </map>
</class>

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:

public class Container {
    private int id;
    private Map<Key,String> values = new HashMap<Key,String>();

    public String getValue(String category, String property) {
        return values.get(new Key(category, property));
    }

    public void setValue(String category, String property, String value) {
        values.put(new Key(category, property), value);
    }

    public static class Key {
        private String category;
        private String property;

        public Key(String cat, String prop) {
            category = cat;
            property = prop;
        }

        public String getCategory() {
            return category;
        }

        public String getProperty() {
            return property;
        }

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Key)) {
                return false;
            }
            Key k = (Key)obj;
            return category.equals(k.category) && property.equals(k.property);
        }

        @Override
        public int hashCode() {
            return 37*category.hashCode() + property.hashCode();
        }
    }
}

Mapping:

<class name="Container" table="container">
    <id column="ID" name="id">
        <generator class="native"/>
    </id>
    <map cascade="all-delete-orphan" name="values" table="example">
        <key column="ID"/>
        <composite-map-key class="Container$Key">
            <key-property name="category" type="string" column="CATEGORY"/>
            <key-property name="property" type="string" column="PROPERTY"/>
        </composite-map-key>
        <element type="string" column="VALUE"/>
    </map>
</class>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文