2

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?

Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

1

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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? – Dejell Jul 20 '10 at 14:48
  • 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 Jul 20 '10 at 14:51
  • 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 Jul 20 '10 at 16:57
1

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

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 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? – Dejell Jul 20 '10 at 17:12
  • @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 Jul 20 '10 at 17:45
  • 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 – Dejell Jul 20 '10 at 20:27