views:

1134

answers:

2

Hi All,

I have a requirement in my application. My tables wont be defined beforehand.For Example,if the user creates a form by name Student,and adds its attributes like name,roll no,subject,class etc.. then on runtime,there should be a table created by name student with columns name,roll no,subject,class and also its related class and its hibernate mapping file. Is there any way of doing so?

Thanks in advance,

Rima Desai

+2  A: 

It's possible, but it's not clear why would you want to do something like that, so it's hard to suggest any specific solution.

But generally, yes, you can generate database tables, hibernate classes and mappings dynamically based on some input. The easiest approach is to use some template engine. I've used Velocity in the past and it was very good for this task, but there are others too if you want to try them.

EDIT:

Following OP clarification the better approach is to use XML to store user defined data. The above solution is good but it requires recompiling the application whether forms are changed. If you don't want to stop and recompile after each user edit, XML is much better answer.

To give you some head start:

@Entity
public class UserDefinedFormData {
    @Id
    private long id;

    @ManyToOne
    private FormMetadata formMetadata;

    @Lob
    private String xmlUserData;
}

Given a definition of the form it would trivial to save and load data saved as XML.

Add a comment if you would like some more clarifications.

Gregory Mostizky
Hi Gregory,Thanks for your suggestion. i would definitely check out velocity. The reason for doing so is we are creating an engine where in the user can create forms and later use them. So while creating the forms ,if the table can and its related class can be created it would become easy to store the data when the forms are used.Rima Desai
I see. I will edit the answer with a better approach for you
Gregory Mostizky
Here's an exercise: say I've added "name", "address" and "postalCode" attributes and stored them within XML lob for a couple million of user records or so - how exactly do I go about getting everybody with first name "Gregory" living in 10 given postal codes and sorting the results by name?
ChssPly76
For simple search Lucene is enough. More complicated stuff can be tougher... but is it even a requirement? My impression of OP was that he just needs to store the data for some custom forms, he doesn't really know or care what's inside of it.
Gregory Mostizky
Lucene is enough to search through XML stored in database? Do explain how that can be done without retrieving ALL records first. I don't know for sure whether searching by custom attributes is a requirement for this particular case (though if you're defining student name as OP specified it's not that big of a reach to imagine that someone would like to search or sort by it), but I do believe that you should at the very least mention the downsides of the approach you're proposing.
ChssPly76
You are wrong. Lucene does not require getting all the records from the database to be able to search on it, the same as Google doesn't require to load all the internet's website for each time you press search button. It uses indexing and is highly efficient at what it does. Also it supports searching by specific attribute and not just full text search. I suggest you read up on Lucene first.
Gregory Mostizky
Oh, so you suggest using Lucene to index marshalled XML as it gets inserted / updated into the database then? Good stuff. No further questions.
ChssPly76
+2  A: 

Hibernate supports dynamic models, that is, entities that are defined at run-time, but you have to write out a mapping file. You should note a couple things about dynamic models:

  1. You may be restricted in how you define these at run-time (viz. you will have to use the Session directly instead of using a helper method from HibernateTemplate or something like that).

  2. Dynamic models are supported using Maps as the container for the fields of an entity, so you will lose typing and a POJO-style API at run-time (without doing something beyond the baked-in dynamic model support).

All of that said, you didn't mention whether it was a requirement that the dynamically defined tables be persistent across application sessions. That could complicate things, potentially.

Paul Morie