Dejell
Dejell

Reputation: 14317

Dynamic fields in JSF beans

I want to let my client create 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?

Upvotes: 2

Views: 1484

Answers (2)

Pascal Thivent
Pascal Thivent

Reputation: 570365

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:

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

Related questions

Upvotes: 1

BalusC
BalusC

Reputation: 1108722

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.

Upvotes: 1

Related Questions