ManyToMany 删除
程序员文章站
2022-07-13 08:17:57
...
@Entity
@Data
@Table(name="dev_network")
public class DevNetwork implements Serializable {
// ID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// user id
private Long uid;
// data
@Column(name = "data",columnDefinition="text")
private String data;
// JoinTable => dev_network + users_networks + user =>
// id = dev_network_id user_id = id
// {id,uid,data} +{dev_network_id,user_id} +{id,create_time,...}
@JsonIgnore
@ManyToMany
@JoinTable(name = "users_networks", joinColumns = {@JoinColumn(name = "dev_network_id",referencedColumnName = "id")}, inverseJoinColumns =
{@JoinColumn(name = "user_id",referencedColumnName = "id")})
private Set<User> users;
...
}
@Entity
@Getter
@Setter
@Table(name="user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull(groups = Update.class)
private Long id;
@Column(name = "create_time")
@CreationTimestamp
private Timestamp createTime;
@Column(name = "last_password_reset_time")
private Date lastPasswordResetTime;
@ManyToMany
@JoinTable(name = "users_roles", joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
private Set<Role> roles;
@ManyToMany(mappedBy="users") // 这里是user表,mappedBy="users" 表示关系由dev_network 维护。
private Set<DevNetwork> networks;
}
// 根据 dev_network 的 id 删除 dev_network,中间表关系也会自动删除
devNetworkRepository.deleteById(id);
@Data
@Table(name="dev_network")
public class DevNetwork implements Serializable {
// ID
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// user id
private Long uid;
// data
@Column(name = "data",columnDefinition="text")
private String data;
// JoinTable => dev_network + users_networks + user =>
// id = dev_network_id user_id = id
// {id,uid,data} +{dev_network_id,user_id} +{id,create_time,...}
@JsonIgnore
@ManyToMany
@JoinTable(name = "users_networks", joinColumns = {@JoinColumn(name = "dev_network_id",referencedColumnName = "id")}, inverseJoinColumns =
{@JoinColumn(name = "user_id",referencedColumnName = "id")})
private Set<User> users;
...
}
@Entity
@Getter
@Setter
@Table(name="user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull(groups = Update.class)
private Long id;
@Column(name = "create_time")
@CreationTimestamp
private Timestamp createTime;
@Column(name = "last_password_reset_time")
private Date lastPasswordResetTime;
@ManyToMany
@JoinTable(name = "users_roles", joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
private Set<Role> roles;
@ManyToMany(mappedBy="users") // 这里是user表,mappedBy="users" 表示关系由dev_network 维护。
private Set<DevNetwork> networks;
}
// 根据 dev_network 的 id 删除 dev_network,中间表关系也会自动删除
devNetworkRepository.deleteById(id);