春季引导映射问题

huangapple 未分类评论49阅读模式
英文:

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 = &quot;Script name cannot be empty&quot;)
@Column(unique = true)
private String scriptName;
private boolean enabled;
@ManyToOne
private User author;
@ManyToMany(cascade = CascadeType.ALL, mappedBy = &quot;ownedScripts&quot;)
private List&lt;User&gt; users = new ArrayList&lt;&gt;();

public class User {
@Id
@Column(updatable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@NotBlank(message = &quot;Username cannot be empty&quot;)
@Column(unique = true)
private String discordUsername;
@NotBlank(message = &quot;Password cannot be empty&quot;)
private String password;
private boolean enabled;
private Role role;
@ManyToMany(cascade = CascadeType.ALL)
private List&lt;Script&gt; ownedScripts = new ArrayList&lt;&gt;();
@OneToMany(cascade = CascadeType.ALL)
private List&lt;Script&gt; createdScripts = new ArrayList&lt;&gt;();

huangapple
  • 本文由 发表于 2020年4月11日 06:15:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61149466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定