Dynamic Typed Table/Model in Java EE?

Usually with Java EE when we create Model, we define the fields and types of fields through XML or annotation before compilation time. Is there a way to change those in runtime? Or better, is it possible to create a new Model based on the user's input during the runtime? Such that the number of columns and types of fields are dynamic (determined at runtime)?

Help is much appreciated. Thank you.

I felt the need to clarify myself.

  1. Yes, I meant database modeling, when talking about Model.
  2. As for the use cases, I want to provide a means for users to define and create their own tables. Infinite flexibility is not required. However some degree of freedom has to be there: e.g. the users can define what fields are needed to describe their product.


I worked on a system where we had such facilities. To stay efficient, we would generate/alter the table dynamically for the customer schema. We also needed to embed a meta-model (the model of the model) to process information in the entities dynamically.

Option 1: With custom tables, you have full flexibility, but it also increases the complexity significantly, notably the update/migration of existing data. Here is a list of things you will need to consider:

  • What if the type of a column change?
  • What if a column is added? Is there a default value?
  • What if a column is removed? Can I discard the existing information?
  • How to manage renaming of a column?
  • How to make things portable across databases?
  • How to make it efficient at database-level (e.g. indexes) ?
  • How to manage a human error (e.g. user removes a column then changes its mind)?
  • How to manage migration (script, deployment, etc.) when new version of the system is installed at customer site?
  • How to have this while using an ORM?

Option 2: A lightweight alternative is to add a few "spare" columns in the business tables of different types (e.g.: "USER_DATE_1", "USER_DATE_2", etc.) I've seen that a few times. It will makes your DBA scream and is not really considered a good practice, but at least can facilitates a few things, e.g. (migration scripts, ORM integration).

Option 3: Another option is to store everything in a table with a structure property/data. But then it's really a disaster for database performance. Anything that is not completely trivial will require many joins. And the DBA will scream even more.

Option 4: It is a mix of options 2 and 3. Core tables are fixed, but a table with property/data can be used to somehow extend them.

In summary: think twice before you go this way. It can be done, but has a significant impact on the design and maintenance of the application.

You sound like you want to be able to change both objects and schema according to user input at runtime. This sounds like a chaotic recipe for disaster to me. I've never seen it done.

I have seen general schemas that incorporate foreign key relationships to generic tables of name/value pairs, but these tend to become infinitely flexible abstractions that can neither be easily understood nor get out of their own way when it comes to performance.

I'm betting that your users really don't want infinite flexibility. I'd caution you against taking this direction. Better to get your real use cases straight.

Anything is possible, of course. My direct experience tells me that it's a bad idea that your users will hate if you can pull it off. Best of luck.

This is somehow possible using meta-modeling techniques:

  • tables for table / column / types at the database level
  • key/value structures at the Java level

But this has obvious limitations (lack of strong typed objects) and can IMHO get quickly very complicated (not even sure how to deal with relations). I wouldn't use this approach to define domain objects entirely, but only to extend existing ones (products, articles, etc).

If I remember well, this is what some e-commerce solutions (e.g. BroadVision) were doing.

I think I have found a good answer myself. Those new no-sql (hbase, cassandra) database seems to be exactly what I was looking for. Thanks everyone for your answeres.

Category:java Time:2010-04-18 Views:0

Related post

  • Defining a drop down combo box within an abstract table model in java 2010-12-29

    Can somebody please inform me as to how to define a drop down combo box as a cell within a row in an abstract table model, new to Java so really not sure how to do this. This will be be the first Cell as shown below E.g Combo Box My code is as follow

  • Scala dynamic typing, type bounds and java objects - is this expected behavior or a bug? 2011-04-05

    Sorry if this has been covered already. The following simple example of dynamic typing and type bounds does not work with a list of strings but works perfectly well with a (scala) class containing a defined length() function. Is this expected behavio

  • Display Table Titles with a Table Model in Java Swing 2010-01-22

    I would like to create a table with the constructor method JTable(TableModel). What exact methods in the TableModel do I need to display the titles of each column? --------------Solutions------------- You need to embed your JTable within a JScrollPan

  • What's the best statically-typed language for a dynamically-typed language programmer to learn? 2008-09-17

    I've been a Ruby developer for several years, and a Perl developer before that, with some PHP somewhere in there. I want to expand my knowledge, and think the best way to do that will be by learning something very different from what I know. I know O

  • Creating a Java Table Model which is initially empty but allows dynamic addition of rows by user 2011-03-15

    I am making an application whose main function is encompassed in a JTable. I need to make an accompanying table model with an addRow method, the user enters details in the window and upon clicking a JButton the data is brought in to an array which is

  • Dynamically create table and java classes at runtime 2009-07-28

    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

  • Besides dynamic typing, what makes Ruby "more flexible" than Java? 2009-04-05

    I've been using Java almost since it first came out but have over the last five years gotten burnt out with how complex it's become to get even the simplest things done. I'm starting to learn Ruby at the recommendation of my psychiatrist, uh, I mean

  • SQL to handle table updates in a "dynamically typed" fashion 2010-08-26

    I'm playing around with Python 3's sqlite3 module, and acquainting myself with SQL in the process. I've written a toy program to hash a salted password and store it, the associated username, and the salt into a database. I thought it would be intuiti

  • rails 3 dynamically create a model with attributes that are stored in a database table 2011-04-21

    I am creating a ruby on rails application which stores a users favourite publications and have stumbled across a problem and need some help. The user is able to add as many different types of publications as they choose, such as books, movies, newspa

  • I Want to Export to Excel dynamic Html table in java 2011-08-16

    I Want to Export to Excel dynamic Html table in java using apache poi --------------Solutions------------- Please specify your needs. You just stated what you want but not a real question. If you want to use Apache POI then go ahead, once you read th

  • Java table model design 2008-10-03

    I have a table class that creates modifies a table of items. I want to display those items in a JTable using a table model. To me table model belongs to my gui package but table needs table model in order to fire changes and table model needs table c

  • OOP and Dynamic Typing (not Static vs Dynamic) 2009-12-16

    What OOP principles, if any, don't apply or apply differently in a dynamically typed environment as opposed to a statically-typed environment (for example Ruby vs C#)? This is not a call for a Static vs Dynamic debate, but rather I'd like to see whet

  • Which languages are dynamically typed and compiled (and which are statically typed and interpreted)? 2010-02-24

    In my reading on dynamic and static typing, I keep coming up against the assumption that statically typed languages are compiled, while dynamically typed languages are interpreted. I know that in general this is true, but I'm interested in the except

  • Is the a pattern for iterating over lists held by a class (dynamicly typed OO languages) 2010-06-05

    If I have a class that holds one or several lists, is it better to allow other classes to fetch those lists (with a getter)? Or to implement a doXyzList/eachXyzList type method for that list, passing a function and call that function on each element

  • Entity/Attribute alternative - dynamically created tables 2009-01-17

    I like pure relational design but sometimes there is a need for an Entity/Value method of storing data. Especially where the user needs to frequently create a new type of data. I have seen in some commercial software that they dynamically create stan

  • Why are dynamically typed languages slow? 2009-04-17

    What makes it hard to speed up dynamically typed languages when compared to statically typed languages . In other words what is inherent property of statically typed languages that make them easy to optimize for speed of execution ? --------------Sol

  • Why Is Dynamic Typing So Often Associated with Interpreted Languages? 2009-09-08

    Simple question folks: I do a lot of programming (professionally and personally) in compiled languages like C++/Java and in interpreted languages like Python/Javascript. I personally find that my code is almost always more robust when I program in st

  • From Static Typing to Dynamic Typing 2009-09-28

    I have always worked on staticly typed languages (c/c++,java). For the poast months i have been playing with clojure and i really like it. One thing i am worried about is, say that i have a windows that takes 3 modules as arguments and along the way

  • How to map dynamically created table in Hibernate? 2009-10-02

    I am working on a web application. We are using Hibernate as ORM in our project. Actually, our application creates some tables dynamically based on user selection. The user can select table name, column name and then s/he can import data from a csv f

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 1.511 (s). 13 q(s)