Hi,
I'm using Hibernate 2.1 and couldn't quite figure out how to write a custom property accessor that is capable of mapping between the properties of a Java class and the database columns where the java class does not have JavaBean style getters/setters for most of the properties.
My Java class basically has the followng structure:
public class Foo {
private String a;
private java.util.Map customPropertiesMap = new java.util.HashMap();
public String getA() { return a; }
public void setA(String a) {this.a = a;}
...
setCustomProperty(String customPropertyName, Object customPropertyValue) {
customPropertiesMap.put(customPropertyName, customPropertyValue);
}
Object getCustomProperty(String customPropertyName) {
return customPropertiesMap.get(customPropertyName);
}
}
As shown above, the class Foo has a property called "a", which has its own getter/setter methods. For this property, I don't need a custom property accessor, since I can simply map it to a database column.
However, I have a set of custom/additional properties that is stored in the customPropertiesMap map which I also need to map to database columns. I use this customPropertiesMap because I need to allow my customers to be able to define their own custom properties (in addition to the built-in properties such as "a") in a way that doesn't require my customer to have to write and compile a Java class (they don't know Java). My customer would simply modify the Hibernate mapping file to define additional properties/columns for Foo class and specify a custom property accessor so that the Hibernate runtime could transfer the data between the object and the table correctly.
However, this simple approach doesn't seem workable because the get(Object target) method defined in net.sf.hibernate.property.Getter class does NOT take a property name as an argument! Instead, the getGetter method in net.sf.hibernate.property.PropertyAccessor class assumes that the returned instance of an implementation of Getter interface is specific to the property (ie, each distinct property has a different implementation of Getter interface), and therefore the name of the property doesn't have to be passed to the get(Object target) method on the Getter.
This is prohibiting me from writing a "generic" getter that works for all "custom" properties for my class Foo. For example, if the get(Object target) method would have taken a property name as an argument, then I could have solved my problem by implementing a Getter as follows:
public class FooGetter implements Getter {
public Object get(Object target, String propertyName) {
Foo foo = (Foo) target;
return foo.getCustomProperty(propertyName);
}
}
Is there any way to do something like this with Hibernate, or is it simply off limit?
Any help would be greatly apprecaited.
/Jong
|