Yoni
Yoni

Reputation: 385

Hibernate persisting an Object member

Is there any way to persist an Entity member of type java.lang.Object ?

lets say i have an entity DynamicProperty which has a members

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 .

Upvotes: 2

Views: 1472

Answers (2)

Yoni
Yoni

Reputation: 385

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.

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

does any one knows regarding the performance if this ? or any other issues that can occur..

Upvotes: 1

Mateusz
Mateusz

Reputation: 3048

If they're basic types, you can always try to store them as Strings and then cast them to right classes in your 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.

Also, take a look at the @Embedded annotation, it may help.

Upvotes: 0

Related Questions