-->
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.  [ 5 posts ] 
Author Message
 Post subject: Dynamic Field additions
PostPosted: Tue Oct 19, 2010 4:57 pm 
Newbie

Joined: Thu Sep 30, 2010 2:22 pm
Posts: 2
Hey, I'm working on a problem where users need to be able to dynamically add new fields/properties (columns) to persistent entities. I've been researching this for awhile now, and seem there were various solutions. The one I'm using now (prototyping) involves directly using the Hibernate API, i.e.

Code:
PersistentClass clazz = config.getClassMapping("some.class.Here");
Column col = new Column();
col.setName("Test");
col.setSqlType("varchar (255)");


I was thinking this would be a way for me to add the columns and properties at runtime (or when the session factory is created.) A few things I have yet to find answers on:
1. How do you set a property on a class you generate at runtime with Hibernate? I'm guessing it involves reflection, but not sure how.
2. Is there a file that defines the SqlType's that are valid to use with Hibernate?
3. Should I be setting the column on the Table? Or should I be creating a Property instead?

The other issue with this problem (I believe) will be updating the database schema when the user adds a new field. I think I can make this work with LiquiBase, but haven't implemented anything yet.

If anyone has any ideas or suggestions on this, I would be grateful. Thanks everyone


Top
 Profile  
 
 Post subject: Re: Dynamic Field additions
PostPosted: Wed Oct 20, 2010 5:50 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
If I were you, I would throw out Hibernate altogether for such use case. Do not think it is capable for anything like that.


Top
 Profile  
 
 Post subject: Re: Dynamic Field additions
PostPosted: Wed Oct 20, 2010 7:11 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
We are doing something similar in our project. There are a lot of things to hook up to get everything working. I'll try to outline what we are doing and post some links to our code. Hope it will help you.

Basically we have an XML configuration that holds admin-defined properties. It is currently limited to a few simple data types (eg. string, int, float, boolean, etc.). The default configuration file looks like this: http://base.thep.lu.se/browser/branches ... erties.xml and it's use is documented here: http://base.thep.lu.se/chrome/site/late ... rties.html

When starting up we read in the XML file and store as ExtendedProperty objects. Those are then used to create Hibernate Property mappings that are programmatically added to a PersistentClass:

http://base.thep.lu.se/browser/branches ... .java#L272 and
http://base.thep.lu.se/browser/branches ... .java#L351

The API provided by Hibernate in this area is not a public API and it has happened that things break between Hibernate versions. The Javadoc is not very complete either so it is a lot of trial-and-error to get things working. Another approach (better?) would be to generate an XML-string identical to a regular hbm.xml file and then add that string to the configuration with Configuration.addDocument(). We are using this approach for some other entities (http://base.thep.lu.se/browser/branches ... .java#L298). I'm not sure why we don't use this approach in the first case as well...

To get/set values for the configurable properties we use a custom PropertyAccessor implementation (http://base.thep.lu.se/browser/branches ... essor.java) that re-direct set/get calls to a generic pair of methods that takes the property name as an argument. Inside the implementation we use a Map to store the values: (http://base.thep.lu.se/browser/branches ... .java#L195

This should complete the outline. I guess there are a lot of details not covered.


Top
 Profile  
 
 Post subject: Re: Dynamic Field additions
PostPosted: Wed Oct 20, 2010 11:45 am 
Newbie

Joined: Thu Sep 30, 2010 2:22 pm
Posts: 2
Thank you both for your responses.

nordborg: Very interesting solution and thanks for the code samples. I am currently looking over the code you linked and will continue to look at how you accomplished this. We are currently using annotations, but it seems that possibly manipulating a mapping file may be easier in the long run to maintain. The approach would be to read in the original mapping file, then add any fields that were defined dynamically, then call the addDocument method?

Was wondering though, how are you updating the database schema when a new column is added to a table? In other words, are you physically altering the table schema to add a new column?

I imagine I'll encountered a few more problems along the way, so hopefully I can draw on your experience :). Thanks again


Top
 Profile  
 
 Post subject: Re: Dynamic Field additions
PostPosted: Wed Oct 20, 2010 1:42 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Was wondering though, how are you updating the database schema when a new column is added to a table?


We are using the schema update tool built into Hibernate. It's more or less:

Code:
new SchemaUpdate(configuration).execute(false, true);


This usually works well for adding columns. It doesn't support removing a column, so that needs to be done manually. As far as I know we have not had any real use case that needed to remove a column.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.