英文:
Spring boot mapping issue
问题
我正在尝试映射User和Script,我的主要目标是允许用户创建多个脚本并拥有多个脚本,
但是Script只能由一个用户创建,并且可以被多个用户拥有。
public class Script {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@NotBlank(message = "脚本名称不能为空")
@Column(unique = true)
private String scriptName;
private boolean enabled;
@ManyToOne
private User author;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "ownedScripts")
private List<User> users = new ArrayList<>();
public class User {
@Id
@Column(updatable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@NotBlank(message = "用户名不能为空")
@Column(unique = true)
private String discordUsername;
@NotBlank(message = "密码不能为空")
private String password;
private boolean enabled;
private Role role;
@ManyToMany(cascade = CascadeType.ALL)
private List<Script> ownedScripts = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL)
private List<Script> createdScripts = new ArrayList<>();
英文:
I'm trying to map User and Script, my main goal is that a user can create many script and own many scripts
but Script can be created only by one user and be owned by many
public class Script {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@NotBlank(message = "Script name cannot be empty")
@Column(unique = true)
private String scriptName;
private boolean enabled;
@ManyToOne
private User author;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "ownedScripts")
private List<User> users = new ArrayList<>();
public class User {
@Id
@Column(updatable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@NotBlank(message = "Username cannot be empty")
@Column(unique = true)
private String discordUsername;
@NotBlank(message = "Password cannot be empty")
private String password;
private boolean enabled;
private Role role;
@ManyToMany(cascade = CascadeType.ALL)
private List<Script> ownedScripts = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL)
private List<Script> createdScripts = new ArrayList<>();
专注分享java语言的经验与见解,让所有开发者获益!
评论