映射非规范化休眠

发布于 2024-08-29 12:19:06 字数 265 浏览 3 评论 0原文

我有一个摘要类,其中包含质量列表。 Quality 包含一个 String 名称和 int 值。此数据存储在非规范化的数据库结构中,仅一个表,用于摘要和质量。

质量表:

id, 一些领域, 质量名称1, 质量值1, 质量名称2, 质量值2, 质量名称3, Qualityvalue3

对于每个质量名称和质量值值对,必须将新的 Quality 对象插入到 Summary 类中。

如何在hibernate中映射它(xml hibernate映射)?

I have a Summary class which contains a list of Qualities. A Quality contains a String name and int value. This data is stored in a denormalized db structure, one table only, for both Summary and Quality.

Quality table:

id,
somefileds,
qualityname1,
qualityvalue1,
qualityname2,
qualityvalue2,
qualityname3,
qualityvalue3

For each quality name & value pairs, a new Quality object must be inserted in the Summary class.

How to map this in hibernate (xml hibernate mapping)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

傲性难收 2024-09-05 12:19:06

目前尚不清楚 Quality 对象必须如何“插入”到 Summary 持有者类中,但我认为自定义用户类型(ohuUserTypeohuUserCollectionType)是这样的去这里。

请参阅 5.2 节。 3.文档中的自定义值类型了解更多详细信息(关于映射无需多言,只需在映射中将自定义用户类型指定为 type 即可)。

It is not clear how Quality objects must be "inserted" in the Summary holder class but I think that a custom user type (either o.h.u.UserType or o.h.u.UserCollectionType) is the way to go here.

Refer to the section 5.2.3. Custom value types in the documentation for more details (there is not much to say about the mapping, simply specify your custom user type as type in the mapping).

爱的故事 2024-09-05 12:19:06

我设法使用自定义 CompositeUserType 实现来修复它。映射文件如下所示:

<property name="qualities" type="com.foo.bar.QualityCompositeUserType">
        <column name="linkName1" />
        <column name="linkQuality1" />
        <column name="linkName2" />
        <column name="linkQuality2" />
        <column name="linkName3" />
        <column name="linkQuality3" />
</property>

CompositeUserType 中的返回类是 List.class,nullSafeSet 方法获取集合作为值参数。只需从列表中获取值并将它们分配给准备好的语句中的参数即可。 (如果质量列表中缺少值,则填充 null)。

nullSafeGet 方法更简单。我只是创建一个新的 ArrayList,其中插入新的 Quality 对象以及结果集中的值。

如果有兴趣,我可以添加一个完整的示例。

I managed to fix it with a custom CompositeUserType implementation. The mapping file looks as follows:

<property name="qualities" type="com.foo.bar.QualityCompositeUserType">
        <column name="linkName1" />
        <column name="linkQuality1" />
        <column name="linkName2" />
        <column name="linkQuality2" />
        <column name="linkName3" />
        <column name="linkQuality3" />
</property>

The returnedClass in the CompositeUserType is List.class, the nullSafeSet method gets the collection as value argument. It is just a matter of getting the values from the list and assign them to the parameters in the prepared statement. (and fill with null if values are missing in the list of Qualities).

The nullSafeGet method is easyer. I just create a new ArrayList in which new Quality objects are inserted with values from the resultset.

I can add a full example if interested.

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