我有兩個具有這些欄位的物體:
代碼物體:
@Entity
@Table(name = "snippets")
@NoArgsConstructor
@Getter
@Setter
public class Code {
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
@Id
@GeneratedValue
private Long id;
private String code;
private String date;
private User author;
private List<User> allowedUsers;
public Code(CodeDto code) {
this.code = code.getCode();
this.date = LocalDateTime.now().format(formatter);
this.author = code.getAuthor();
}
}
用戶物體:
@Entity
@Table(name = "users")
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class User implements UserDetails {
@Id
@GeneratedValue
private Long id;
private String username;
private String password;
@Enumerated(EnumType.STRING)
private UserRole userRole;
@Transient
private boolean locked;
private boolean enabled = true;
private List<Code> codeList;
public User(String username, String password, UserRole userRole) {
this.username = username;
this.password = password;
this.userRole = userRole;
}
}
我正在嘗試創建一個類似于 github 的應用程式(但當然非常簡化)。我希望每個用戶都能夠添加一個代碼片段,然后將其訪問權限授予其他用戶。我想存盤誰是作者以及誰有權訪問每個代碼片段的資訊。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
- 每個代碼都可以被用戶串列看到:這可以用 OneToMany 關系表示。
- 每個用戶都有一個代碼串列:這也用 OneToMany 關系表示。
這些關系可以用這個來表示:
@OneToMany @JoinColumn(name = "code_id", referencedColumnName = "id")
private List<Code> codeList;
另一個也可以這樣做:
@OneToMany @JoinColumn(name = "user_id", referencedColumnName = "id")
private List<User> allowedUsers;
uj5u.com熱心網友回復:
我希望每個用戶都能夠添加代碼片段
我假設您實際上是指多個代碼片段,這會更明智。要實作這一點,您需要在用戶和代碼之間建立一個 OneToMany 關聯,為此您將使用 @OneToMany。
您還沒有提到您正在使用的持久性提供程式,但至少對于 Hibernate,我建議使用雙向關系以避免在添加代碼時進行額外的更新查詢。
當關系是雙向的時,關系的 @ManyToOne 端,也就是所有者端,負責填充物體插入的外鍵列,更多在這里。
public class User implements UserDetails {
...
@OneToMany(mappedBy = "writer")
private Set<Code> codeList = new HashSet<>();
public void addCode(Code code) {
codeList.add(code);
code.setWriter(this);
}
public void removeCode(Code code) {
codeList.remove(code);
code.setWriter(null);
}
}
正如我們所說,關聯是雙向的,因此在代碼方面,您將擁有:
public class Code {
...
@ManyToOne(fetch = FetchType.LAZY)
private User writer;
}
PS1:我將串列切換為設定,因為它是推薦的,更多在這里。
PS2:處理雙向OneToMany 關系可能很棘手,即在我們的示例中忘記設定writer 端將導致資料庫中的外鍵為空。避免這種情況的一個好方法是使用輔助方法來添加和洗掉子物體。
然后將其訪問權限授予其他用戶
為此,關聯將是@ManyToMany,以便將其映射到資料庫中的連接表:
public class Code {
...
@ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
@JoinTable(name = "code_viewers",
joinColumns = @JoinColumn(name = "code_id"),
inverseJoinColumns = @JoinColumn(name = "viewer_id"))
private Set<User> viewers = new HashSet<>();
}
再一次,您需要設定關系的另一側:
public class User implements UserDetails {
...
@ManyToMany(mappedBy = "viewers")
private Set<Code> accessibleCodes = new HashSet<>();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335934.html
上一篇:在spring資料jpa中通過@Query更新Entity內的關聯物件
下一篇:序列不在oracle表上的一行中
