如何使用 Hibernate 保留 Object 类型的 Java 属性? [英] How do I persist a Java property of type Object using Hibernate?

查看:29
本文介绍了如何使用 Hibernate 保留 Object 类型的 Java 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有这样属性的 Java 类

I have a Java class with a property like this

private Object myObj;

当我尝试使用 Hibernate 注释保存类时,我收到了相当令人困惑的错误消息属性映射的列数错误".

When I try to save the class using Hibernate annotations, I get the rather confusing error message "property mapping has wrong number of columns".

持久化包含此类通用属性的类的正确方法是什么?

What is the correct approach to persisting a class containing a generic property such as this?

推荐答案

如您所说,对象可以是图像、视频等.

As you said, the object can be image, video, and more.

如果它应该只有二进制数据,你可以创建另一个类,并从这个类传输数据.在传输过程中可以将Object转换为byte[](取决于具体的类型),并使用Lob数据类型(@Lob)用于映射它.

If it is supposed to have only binary data, you can create another class, and transfer the data from this one. In the process of transferring you can convert the Object to byte[] (depending on the concrete type), and use a Lob data type (@Lob) for mapping it.

为了扩展这一点,如果不仅要支持二进制数据,您的新对象可以有 2 个字段 - 一个用于二进制,一个(或多个)用于其他类型.

To extend this, if not only binary data is to be supported, your new object can have 2 fields - one in case of binary, and one (or more) in case of other types.

无论如何,现在的对象代表了一个相当不智能的设计,它无法毫无困难地正确持久化.

Anyway, the object as it is now, represents a quite unintelligent design, and it cannot be persisted properly without hassle.

这篇关于如何使用 Hibernate 保留 Object 类型的 Java 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆