-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How can I use one "generic" Getter for a set of pr
PostPosted: Tue Aug 24, 2004 3:55 pm 
Newbie

Joined: Tue Aug 24, 2004 3:20 pm
Posts: 6
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 5:37 pm 
Newbie

Joined: Tue Aug 24, 2004 3:20 pm
Posts: 6
Does anyone care to answer this question, please? Or else tell me I'm missing something.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 5:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I'd rather take a look at dynamic-component. Sounds exactly like what you describe.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.