简体   繁体   English

Hibernate持久化一个Object成员

[英]Hibernate persisting an Object member

Is there any way to persist an Entity member of type java.lang.Object ? 有什么办法可以保留类型java.lang.Object的Entity成员?

lets say i have an entity DynamicProperty which has a members 可以说我有一个拥有成员的实体DynamicProperty

private String name;
private Object value;

Value can be of several types basically non complex ones (String, Boolean, Integer, Decimal, Enum...) 值可以是几种基本不复杂的类型(字符串,布尔值,整数,小数,枚举...)

is there any way doing it ? 有什么办法吗? and what the DB(Oracle) column type should be . 以及DB(Oracle)列类型应为。

If they're basic types, you can always try to store them as Strings and then cast them to right classes in your getter. 如果它们是基本类型,则始终可以尝试将它们存储为String,然后将其强制转换为getter中的正确类。

There's also another option - you can create a class which will hold value type and will have fields of necessary types, only one being set though. 还有另一个选择-您可以创建一个将包含值类型并具有必要类型的字段的类,尽管只设置了一个。 Something like 就像是

class Foo
{
    Integer a;
    Double b;
    String c;
    int type;

    //getters and setters

    public Object getObject()
    {
        if (type == 1)
            return a;
        else if (type == 2)
            return b;
        return c;
    }
}

It's a bit of a workaround, but it should work. 这是一个变通办法,但应该可以。 I think it isn't possible to persist an Object as abstract. 我认为不可能将Object持久化为抽象。

Also, take a look at the @Embedded annotation, it may help. 另外,看看@Embedded注释,它可能会有所帮助。

i found a solution...as my my Object can be defined as several limited types, basic type i flagged the Object as serializable DB column as BLOB and it worked. 我找到了解决方案...因为我的对象可以定义为几种有限的类型,基本类型我将对象标记为可序列化的DB列为BLOB,并且可以正常工作。

@Type(type = "serializable")
private Object value;

does any one knows regarding the performance if this ? 有没有人知道这件事的表现? or any other issues that can occur.. 或可能发生的任何其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM