tags:

views:

389

answers:

1

I am writing a Hibernate NamingStrategy for our schema.

Our legacy schema (for some reason!) includes type information in column names. For example:

strproperty
intcustomer

I am implementing the following method from the NamingStrategy interface:

public String propertyToColumnName(String propertyName)

I would like to know the Java type of the property above, so that I could implement our naming strategy such that:

public String getProperty() {
    return this.property()
}

would map to strproperty automatically.

For arguments sake say this is the way I would envisage it working (I know this method does not exist):

public String propertyToColumnName(Object propertyName, Class propertyType) {
    if (propertyType instanceof String) {
        return "str" + propertyName;
    } else {
        return "int" + propertyName;
    }
}

Is there any way to ascertain the type of the Java property from within the naming convention hooks in Hibernate?

Thanks in advance.

+1  A: 

I don't think the interface is designed with that in mind - at first glance it doesn't seem like there's any way to know what class the caller of propertyToColumnName() is enquiring about

However, I note that the javadocs for propertyToColumnName() say that it returns "...a column name for a property path expression". It might be worth investigating what String gets passed in to this method by Hibernate: if it is actually fully qualified with the class name then you could parse out the class name and use reflection on that class to determine the property type.

Alternatively, don't forget you can always specify column names directly in your mapping files (or in your annotations if you use them, I guess):

<property name="blah" column="intblah"/>

which might be a simpler approach.

alasdairg
The property path is relative to the class, in other words you just get the property name unfortunately.I'm using explicit mappings too (I'm using annotations) but the less explicit column mappings the better, which is why I'm trying to get as much work done in the naming strategy.Thanks for your response!
bowsie