views:

82

answers:

1

Okay, so I have torn what I had down and am rebuilding it, here's what I need to do.

I have an entity called Property with:

String name;

@ManyToOne EntityType type;

??????? value

I need to store a Value too, but depending on the Type, the value could be either a String, a Double, or a link to another object (of Class type.getJavaClass()).

I tried to do this with inheritance all sorts of ways but still need to be able to do new Property() and have it set the value to null until a Type gets selected, when the appropriate kind of Type gets selected.

What would be the prettiest would be to have the Value be type Object (that way I could put either a String, Double or type.getJavaClass() there, but hibernate won't let me do it. :(

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

A: 

The problem is Hibernate needs to be able to map it to the database so a type of Object doesn't really work.

I would recommend that you make value of type String. Then wrap the "get" and "set" calls so that the end caller is unaware. For example:

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 ) 
    }
    ....

}

}

As for a link to another object, due you mean a link to another table? In which case I would probably make a separate column for each table so you can let the database maintain the referential integrity. Then you can just enhance the getValue() , setValue() to get the appropriate entity.

ccclark
Yes very good, that is basically what I have muddled into place, getTextValue() and getNumericalValue() and getEntityValue() all inherited from ValueType.I hope that I don't see performace issues with the one giant table that all this inheritence has put things in, but it sure is beautiful OO.Thanks!Joshua

related questions