【问题标题】:Spring Boot + JPA (Hibernate) - how to update some fieldsSpring Boot + JPA (Hibernate) - 如何更新一些字段
【发布时间】:2021-02-01 14:18:13
【问题描述】:

我必须更新我的实体 - 当placeA 或/和placeB 更新时,我还必须更新points。 所以我从数据库中获取路由对象并修改一两个字段(placeAplaceB)。问题是我必须相应地更新points -> 在Point 对象中我还必须更新pointAddresspoint.pointAddress 必须更新为route.placeAroute.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 -&gt; point -&gt; pointDetail -&gt; point,问题是是否只更新我的route.point 对象中的pointAddress 就足够了,还是我还必须更新route.point.pointDetail.point 中的pointAddress

【问题讨论】:

    标签: spring-boot hibernate spring-data-jpa


    【解决方案1】:

    首先:point -&gt; pointDetail -&gt; point 不是课程依赖。 pointDetail和point之间的关系是BiDirectional依赖。

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-10-18
        • 2016-01-28
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-04
        • 2016-03-08
        相关资源
        最近更新 更多