【发布时间】:2020-10-02 07:46:51
【问题描述】:
我有一个user 表和一个city 表,还有一个连接表users_cities(列:id、user_id、city_id)。一个用户可以关注多个城市。
从客户端我发送cityIds 的数组。有些可能是新的,有些可能仍被选中,有些可能已被取消选中。
用数据更新users_cities 表的最佳做法是什么?我应该删除该特定userId 的所有内容并插入新数组还是...?~
另外,delete 和 insert 分别如何批量获取数据,以供多对多参考?!
@Getter
@Setter
@NoArgsConstructor
@ToString
@Accessors(chain = true)
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String email;
private String password;
private Boolean isGuest;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Role> roles;
@ManyToOne()
@JoinColumn(name = "country_following_id")
private Country countryFollowing;
@ManyToMany()
private Set<City> citiesFollowing;
@ManyToMany()
private Set<Offer> offersFollowing;
}
和
@Getter
@Setter
@NoArgsConstructor
@ToString
@Accessors(chain = true)
@Entity
@Table(name = "cities")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NonNull
private Long id;
private String countryCode;
private String name;
@Latitude
private Double latitude;
@Longitude
private Double longitude;
}
【问题讨论】:
标签: spring-boot jpa spring-data-jpa jpql