gpt4 book ai didi

java - 在Spring Boot应用程序中从Enums动态创建列

转载 作者:行者123 更新时间:2023-12-01 16:41:28 25 4
gpt4 key购买 nike

我有一个Enum NotificationType。我想使用枚举常量作为列名创建实体“ User”。
目前,我必须手动编写列名称。有什么方法可以自动从枚举创建列。

NotificationType.java

public enum NotificationType {
EVENT,
HELP,
HOLIDAY
}


User.java

import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class User {

@Column(nullable = false)
private String EVENT;

@Column(nullable = false)
private String HELP;

@Column(nullable = false)
private String HOLIDAY;
}

最佳答案

不用,而是创建一个“ user_notifications”表-例如

CREATE TABLE user_notifications
(
user_id UUID not null,
notification_type varchar(255) not null,
timestamp timestamp not null,
payload text not null
);
ALTER TABLE user_notifications ADD CONSTRAINT user_notifications_user_id FOREIGN KEY (user_id) REFERENCES users (user_id);


“ payload”可以是JSON,也可以是标志,无论您想要什么-但JSON最灵活,因为您可以在不更改数据库架构的情况下向通知中添加更多信息。

关于java - 在Spring Boot应用程序中从Enums动态创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com