views:

62

answers:

2

Hi!

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?

+1  A: 

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.

BalusC
I didn't understand the answer.Let's say that my user wants to create a new object named: Order with custom fields: name, time, username.How can I create a completely new bean ? I know that I can iterate over the results, but how can I create a new bean based on DB information as well as a Hibernate object?
Odelya
Then go for the last suggestion. Follow the links, they contains tutorials. Not meant to be harsh, but given your knowledge shown as far, this is going to be a long journey. That's why I suggested an easy and more abstract solution as well as 1st suggestion.
BalusC
After a second thought, aren't you actually reinventing/homegrowing a CMS? I'd rather look for existing CMS'es as Pascal mentioned. Hippo, Alfresco, Nuxeo, Liferay, etc.
BalusC
+1  A: 

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

Pascal Thivent
Thank you. My question is, once we have already used Hibernate + Hibernate Search, do you think that it worth while to switch to MyBatis or another O/R Mapping?
Odelya
@Odelya: Well, I guess not. Actually, I rewrote almost entirely my answer since Hibernate has already been used to implement an EAV approach (so regardless of my opinion, this is doable). But, as I wrote, I wouldn't write yet another CMS and reuse an existing one offering the feature you're looking for.
Pascal Thivent
Thank you.The ability to add/remove fields is only one small part of my application. So I guess that I will have to integrate my code
Odelya