Hi folks,
I hope somebody can help me with this. Basically, I have a user requirement that they be able to "add" fields to my objects after the product ships. Historically I've solved this problem with metadata driven systems where the user could just modify the metadata and their meta-object would change.
Recently though I've fallen in love with hibernate (it solves all the ugly problems that usually eat months of development time). So I'd like to use hibernate to achive my goals. My approach to date was to use POJOs and hibernate as my "core" datatypes and then let my users extend my POJOs via metadata. I then planned to "glue" the user extensions onto my objects by using hibernate's metadata manipulation capabilities.
However, the only reference material I've been able to find on that capability references hiberate 2.1, and I'm on hibernate 3.0. Some of the methods have been depricated since then and I can't figure out how to use the new ones.
Speficially, I'm looking at this code which I more or less cribbed from "Hibernate in Action".
Configuration cfg = HibHelper.getConfiguration();
PersistentClass userMapping;
userMapping = cfg.getClassMapping(User.class.getName());
Column c = new Column();
c.setName("pet");
c.setNullable(true);
c.setUnique(false);
** c.setType(Hibernate.STRING); // depricated in 3.0?
userMapping.getTable().addColumn(c);
SimpleValue v = new SimpleValue();
v.setTable(userMapping.getTable());
v.addColumn(c);
** //v.setType(Hibernate.STRING); depreicated in 3.0?
v.setTypeName(Hibernate.STRING.getName());
Property p = new Property();
p.setValue(v);
p.setName("Pet");
userMapping.addProperty(p);
Specifc questions:
Above, I marked a line of code with a ** because that particular method appears depricated in 3.0. There's a new Column.setTypeIndex() method that takes an int as its arg, but I'm not sure what int cooresponds to each Hibernate type. Can anyone help me on this one?
Secondly and probably more awkwardly, I'm a bit stumped on how to *use* my new property in code. If I want to create a user object with a pet named "fred", how do I tell hibernate that the user's pet is named fred?
If I instantiate a "core" User e.g. User u = new User(), it won't have a property named "pet".
If I proxy the User and add a pet method to it, will hibernate still be able to work with it? Or will I just totally narf Hibernates own reflection code if I pass it a dynamic proxy instead of a POJO?
Alternately, is there some other approach I should be taking here? Reading through the doc it sure looks like other people have solved this problem, but I'm just not seeing my way through to the end yet.
Any help or suggestions would be appeciatded.
|