Dynamic fields in JSF beans
I want to let my client c开发者_StackOverflow中文版reate his own fields and bean in the CMS dynamically.
As well, once he creates a form, I need to create an Hibernate Entity that could be saved to the database.
Is there a way to do it?
I am using JSF2 and Hibernate 3
With recompiling and without?
Creating tables and entities dynamically is IMO not a good idea. Hibernate is not really made for that and generating entities is only a small part of the problem. You would have to add them to the configuration, rebuild a session factory, update the model. And what about subsequent restarts of the application? Not recommended, just forget this approach...
Another option would be to use an Entity-Attribute-Value (EAV) model. This is something many CMS are doing. I've never implemented this with Hibernate but it's doable (and has already been done). Here are some resources:
- Adding new persistable classes at runtime
- [hibernate-dev] dynamic entities (has some sources attached)
But to be honest, I wouldn't implement my own CMS but rather reuse an existing one. One Hippo seems to be a candidate.
See also
- The CCK buzz (Content Creation Kit) and the EAV problem
Related questions
- Entity Attribute Value Database vs. strict Relational Model Ecommerce question
- Approach to generic database design
- How do you build extensible data model
- I am looking for something similar to the drupal CCK, but in Java (in a Java CMS)?
- Java Frameworks that support Entity-Attribute-Value Models
- Implementing EAV pattern with Hibernate for User -> Settings relationship
Easiest way would be using a List<String>
for the field names and a Map<String, Object>
for the field values. Maps can be accessed in EL using dynamic keys like so:
<ui:repeat value="#{bean.fieldnames}" var="fieldname">
<h:inputText value="#{bean.fieldvalues[fieldname]}" /><br />
</ui:repeat>
A completely different alternative is to autogenerate classes using tools like ASM/Javassist and creating database tables on the fly. But that's a lot more work.
精彩评论