【发布时间】:2021-02-01 14:18:13
【问题描述】:
我必须更新我的实体 - 当placeA 或/和placeB 更新时,我还必须更新points。
所以我从数据库中获取路由对象并修改一两个字段(placeA,placeB)。问题是我必须相应地更新points -> 在Point 对象中我还必须更新pointAddress(point.pointAddress 必须更新为route.placeA 或route.placeB 值):
public class Route{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "place_a_id")
private Address placeA;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "place_b_id")
private Address placeB;
@OneToMany(mappedBy = "route", cascade = CascadeType.ALL)
List<Point> points;
public class Point{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "point", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<PointDetail> pointDetails;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "route_id", nullable = false)
private Route route;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "route_address_id", nullable = false)
private Address pointAddress;
public class PointDetail{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "point_id", nullable = false)
private Point point;
我从 db 中获取 Route 实体,从现在开始,该对象处于持久状态(同一事务中的所有内容),因此我不需要显式调用 repository.save(myChangedRoute)。
问题是如何更新route.points[0].pointAddress?
够了吗?
Route route = repository.findRouteById(1L);
route.setPointA("new place");
route.getPoints().get(0).setPointAddress("new place")
route.getPoints().get(0).getRoute() 和 route.getPoints().get(0).getPointDetails() 对象呢?例如,我还应该更新route.getPoints().get(0).getPointDetails() 对象吗? PointDetail 对象有一个字段 point 可能也应该更新?
我的 Route 对象(依赖项)中有相关(嵌套)对象,所以我的问题是如何正确更新我的对象结构,以免用未更新的旧嵌套值覆盖新值,例如我更新了:
route.getPoints().get(0).setPointAddress("new place")
但是我还没有更新route.getPoints().get(0).getPointDetails().get(0).setPoint(MY NEW UPDATED AND NOT YET SAVED Point object)???
所以我们有一个循环依赖route -> point -> pointDetail -> point,问题是是否只更新我的route.point 对象中的pointAddress 就足够了,还是我还必须更新route.point.pointDetail.point 中的pointAddress?
【问题讨论】:
标签: spring-boot hibernate spring-data-jpa