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")