Dynamic typed table / model in Java EE?
Typically with Java EE, when creating a model, we define fields and field types via XML or annotation before compile time. Is there a way to change them at runtime? Or better, is it possible to create a new model based on user input at runtime? So the number of columns and field types is dynamic (determined at runtime)?
Help is greatly appreciated. Thanks.
I felt the need to clarify myself.
-
Yes, I was referring to database modeling when talking about model.
-
In terms of use cases, I want to give users the ability to define and create their own tables. No endless flexibility required. However, there should be some degree of freedom: users can define which fields are needed to describe their product.
a source to share
I worked on a system where we had such capabilities. To stay efficient, we will generate / modify the table dynamically for the client schema. We also needed to embed a metamodel (model model) to dynamically process information in objects.
Option 1: . With custom tables, you have complete flexibility, but also significantly increase the complexity, especially updating / migrating existing data. Here's a list of things you will need to consider:
- What if the column type changes?
- What if a column is added? Is there a default?
- What if a column is deleted? Can I opt out of existing information?
- How do I manage column renaming?
- How do you make things portable across databases?
- How to make it efficient at the database level (like indexes)?
- How to manage human error (for example, a user deletes a column and then changes his mind)?
- How to manage migration (script, deployment, etc.) when a new version of the system is installed on the client site?
- How do I do this when using an ORM?
Option 2: An easy alternative is to add several "spare" columns in different types of business tables (for example: "USER_DATE_1", "USER_DATE_2", etc.). I've seen this several times. This will make you scream DBA and is not really considered good practice, but it can at least make things easier, for example. (migration scenarios, ORM integration).
Option 3: Another option is to store everything in a table with structure / structure data. But then it is really a disaster for database performance. Anything that is not completely trivial will require a lot of joins. And the DBA will scream even more.
Option 4: . This is a combination of options 2 and 3. The underlying tables are fixed, but the property / data table can be used to extend them.
In short: think twice before going this route. This can be done, but has a significant impact on the development and maintenance of the application.
a source to share
Sounds like you want to change both objects and schema to match user input at runtime. This sounds like a chaotic recipe for disaster to me. I have never seen this done.
I've seen generic schemas that involve foreign key relationships with generic name / value pair tables, but they tend to become infinitely flexible abstractions that can't be easily understood and don't go out of their own way when it comes to performance.
I'm sure your users really don't want infinite flexibility. I would warn you against this. It's best to use your real-world use cases.
Anything is possible, of course. My direct experience tells me that this is a bad idea that your users will hate if you can pull it off. Good luck.
a source to share
This is possible using metamodeling techniques:
- tables for table / column / types at the database level
- Java level key / value structures
But this has obvious limitations (no strong typed objects) and can IMHO quickly get very complicated (not even sure how to deal with the relationship). I would not use this approach to fully define domain objects, but only to extend existing ones (products, articles, etc.).
If I remember well, this is what some e-commerce solutions (like BroadVision) have been doing.
a source to share