File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Runtime Creating Tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Runtime Creating Tables" Watch "Runtime Creating Tables" New topic
Author

Runtime Creating Tables

Sanket L Shah
Ranch Hand

Joined: May 08, 2009
Posts: 49
Hello All,

In my application i need to create Table Dynamically. I mean user will create new table from user interface.

I am having two classes named : Table and Column

I have created the proper mapping file for both and tested. And it works fine.

Now suppose, my Table class is having 2 columns object in columns (List<Column>). And each column is having Table object.


I'm using MySQL.

How can i solve the above problem?

Please help me out!!!


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What ORM tool (if any) are you using?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sanket L Shah
Ranch Hand

Joined: May 08, 2009
Posts: 49
Paul Sturrock wrote:What ORM tool (if any) are you using?


Thanks Paul, I am using Hibernate.
I am using MySQL.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate doesn't really support dynamic table creation. You can use hbm2ddl to generate DDL from mappings (or vice versa), but this is at SessionFactory creation time, not normal runtime. You can run native SQL to do your table creation but using that table at runtime is going to be fairly difficult to do. I think you could do it by performing your DDL via native SQL, running hmb2ddl to generate mapping files, compiling these dynamically then reconfiguring your SessionFactory with them. Sounds like a difficult (and dangerous - any user generated DDL is a security hole) thing to do.
Sanket L Shah
Ranch Hand

Joined: May 08, 2009
Posts: 49

Hi Paul,

I think you understood it in the wrong way! What I mean is, when i create a table object with the name student, it should create new Database Table with the name Student and the Database Columns as defined in the columns List.

Thanks Paul!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So you are not trying to create a table in a database? When you say "it should create a new database table with the name student..." etc. do you mean you want to add a new row to an existing table called Table and two Rows in a table called Columns?
Sanket L Shah
Ranch Hand

Joined: May 08, 2009
Posts: 49
Hi Paul,

I want to create new Table with the name student and it will have two columns named column 1 and column 2. It will be a new physical table in the database.



Thanking you!


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sanket L Shah wrote: Hi Paul,

I want to create new Table with the name student and it will have two columns named column 1 and column 2. It will be a new physical table in the database.



Thanking you!




OK. Then please refer to my earlier answer.
Sanket L Shah
Ranch Hand

Joined: May 08, 2009
Posts: 49

Hi Paul,
Yes, I have read again your previous post, and I have come to know that it is very defficult create table dynamically.
But if I wirte native SQL code then if in future If client want to change the DB form MySQL to some other Database then it will create problems. Thanks for all your replies.

Thanks!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So instead dynamically create your mapping file for the new entity and run hbm2ddl to generate the ddl (since this will give you database agnostic SQL). You'll still need to ditch your existing SessionFactory and reconfigure a new one to use the new table. Still seems like an awkward requirement though - how is anyone going to use this dynamically created table? Why does it need to be a seperate entity?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime Creating Tables