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

查看:247
本文介绍了我如何坚持使用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.

如果它应该只有二进制数据,可以创建另一个类,并从这个传送数据。在转移,你可以转换对象的过程字节[] (取决于具体类型)和使用映射成为一个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.

无论如何,它是现在,再presents相当愚蠢的设计,并且它不能被毫不费力正确持久对象

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天全站免登陆