問題描述
在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)
我正在用 Java 中的 Spring Framework 開發一個 API,我必須在運行時在我的表中創建一個字段,如下所示:用戶保存一些信息(如一個簡單的字符串),這些信息變成一個字段表。
如果你有一些解決方案,請告訴我。
@Entity
@Table(name = "tb_myentity")
public class MyEntity {
//fields, but dynamic in runtime!
}
注意:我不想用 field_name 創建一個表並製作一種關係。必須是動態字段。
參考解法
方法 1:
I would say that the answer to this is basically the same as this question even though you're using Spring instead of Hibernate as your data access framework. You'll want to just manually execute the DDL statements for ALTER TABLE ADD COLUMN
to add the column, but your Entity class wouldn't dynamically have more member variables created.
(by Rhadamez Gindri Hercilio、C. Smith)