how to add a column dynamically in hibernate 4 wit

2019-09-10 05:00发布

问题:

I am currently building a e-commerce website using spring mvc4 hibernate4 and Mysql where the Admin should be able to dynamically add a column to a particular table when the application is already running and the server should not be restarted.For example if a new value is inserted in table 1 then a column should be created for it in table 2.

  1. Is there any way to configure hibernate 4 with JPA to dynamically add a column at runtime?
  2. And if yes can you give me a sample code?
  3. And if no what are the alternate ways of achieving my goal of dynamically adding columns?

回答1:

I think there can be no JPA/Hibernate solution for this, since your Entities map to your database schema and what you are trying to achieve is simultanously mutating your Entity's Class and the corresponding schema. You need to go with JDBC and plain SQL for this and use DDL statements.

I wonder what's your use case for this?