Help in solving the problem with hibernation ... leads please! :)

Ok so I rip what I had and rebuild it, that's what I need to do.

I have an object called Property with:

String name;

@ManyToOne EntityType;

??????? Value

I also need to store the value, but depending on the type, this value can be either a string, a double, or a reference to another object (of the Class.getJavaClass () class).

I've tried to do this with inheritance in various ways, but still should be able to do a new property () and set to null until a type is selected, when the appropriate Type is selected.

What would be the nicest thing about being a Type of type Object (so I could put either String, Double, or type.getJavaClass () in there, but hibernate won't let me do that. :(

Any ideas or advice on a good way to achieve this would be appreciated! Thanks, Joshua

0


a source to share


1 answer


The problem is that Hibernate needs to be able to map it to the database, so the object type doesn't really work.

I would recommend that you make the value a String. Then wrap up the "get" and "set" calls so the end caller doesn't know. For instance:



public class Property
{

String name;

Enum Type;
String valueString;




public object getValue()
{
    if( getType() == Long )
    {

    return Long.valueOf( getStringValue() );
    }

    if( getType() == String )
    {
        return valueString;
    }

    .....

}

public void setValue( obj )
{
    if( obj instanceof Long )
    {
    setStringValue( obj.toString() )
        setType( Long ) 
    }
    ....

}

}

      

How to reference another object because you mean a reference to another table? In this case, I'll probably make a separate column for each table so that you can enable the database to maintain referential integrity. Then you can just increment getValue (), setValue () to get the corresponding object.

0


a source







All Articles