Spring boot 弹簧靴&x2B;JPA(Hibernate)-如何更新某些字段

Spring boot 弹簧靴&x2B;JPA(Hibernate)-如何更新某些字段,spring-boot,hibernate,spring-data-jpa,Spring Boot,Hibernate,Spring Data Jpa,我必须更新我的实体-当placeA或/和placeB更新时,我还必须更新点。 所以我从数据库中获取route对象并修改一个或两个字段(placeA,placeB)。问题是我必须相应地更新点,在点对象中,我还必须更新点地址(点.点地址必须更新为路由.placeA或路由.placeB值): 我从db中获取Route实体,从现在起,这个对象处于持久状态(同一事务中的所有内容),因此我不需要显式调用repository.save(MyChangedRoote) 问题是如何更新路由点[0].pointAd

我必须更新我的实体-当
placeA
或/和
placeB
更新时,我还必须更新
。 所以我从数据库中获取route对象并修改一个或两个字段(
placeA
placeB
)。问题是我必须相应地更新
,在
对象中,我还必须更新
点地址
点.点地址
必须更新为
路由.placeA
路由.placeB
值):

我从db中获取
Route
实体,从现在起,这个对象处于持久状态(同一事务中的所有内容),因此我不需要显式调用
repository.save(MyChangedRoote)

问题是如何更新路由点[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()
object吗
PointDetail
对象有一个字段
point
,该字段可能也应该更新

我的路由对象(依赖项)中有相关(嵌套)对象,因此我的问题是如何正确更新对象结构,以避免用未更新的旧嵌套值覆盖新值,例如,我已更新:
route.getPoints().get(0).setPointAddress(“新位置”)

但是我还没有更新
route.getPoints().get(0).getPointDetails().get(0).setPoint(我新更新但尚未保存的点对象)


因此,我们有一个循环依赖项
route->point->pointDetail->point
,问题是在我的
route.point
对象中只更新
pointAddress
是否足够,或者我还必须在
route.point.pointDetail.point
中更新
pointAddress

Hibernate具有一级缓存它确保每个会话只加载一次对象。因此,不会发生覆盖。当然,这取决于您和您的代码如何更新属性以及更新顺序。

首先:
点->pointDetail->point
不是课程依赖项。pointDetail和point之间的关系是双向依赖关系

PointDetail对象有一个可能也应该更新的字段点?当然不是

问题是只更新route.point中的pointAddress是否足够?是的,够了

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;
Route route = repository.findRouteById(1L);
route.setPointA("new place");
route.getPoints().get(0).setPointAddress("new place")