Reputation: 2220
I have to update my entity - when placeA
or/and placeB
is updated, I have to also update points
.
SO I fetch route object from database and modify one or two fields (placeA
, placeB
). The problem is that I have to update points
accordingly -> in Point
object I have to also update pointAddress
(point.pointAddress
has to be updated to route.placeA
or route.placeB
values):
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;
And I fetch Route
entity from db and from now this object is in persistent state (everything within the same transaction) so I don't need to invoke repository.save(myChangedRoute)
explicitly.
The question is how to update route.points[0].pointAddress
?
Is it sufficient?
Route route = repository.findRouteById(1L);
route.setPointA("new place");
route.getPoints().get(0).setPointAddress("new place")
And what with route.getPoints().get(0).getRoute()
and route.getPoints().get(0).getPointDetails()
objects? For example should I also update route.getPoints().get(0).getPointDetails()
object? PointDetail
objects have a field point
that maybe should be also updated?
I have related (nested) objects in my Route object (dependencies) so my question is how to update my object structure properly to not overwrite new values with the old nested ones that are not updated, e.g. I updated:
route.getPoints().get(0).setPointAddress("new place")
but I haven't updated route.getPoints().get(0).getPointDetails().get(0).setPoint(MY NEW UPDATED AND NOT YET SAVED Point object)
???
SO we have a circular dependencies route -> point -> pointDetail -> point
and the question is if it's sufficient to update only pointAddress
in my route.point
object or I have to also update pointAddress
in route.point.pointDetail.point
?
Upvotes: 0
Views: 162
Reputation: 662
First of all: point -> pointDetail -> point
is not a curricular dependecny. The relation between pointDetail and point is BiDirectional dependency.
PointDetail objects have a field point that maybe should be also updated? Of course not.
and the question is if it's sufficient to update only pointAddress in my route.point? Yes that is enough.
Upvotes: 1
Reputation: 721
Hibernate has the first level cache it ensures that objects will be loaded only once per session. So no override can occur. Of course, is up to you and your code how will you update properties and in which order.
Upvotes: 0