I have a parent class:
@Entity(name = "Master")
@Table(name = "master")
public class Master {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "master", cascade = CascadeType.ALL, fetch = FetchType.Lazy)
List<AdditionalAttribute> additionalAttributes = new ArrayList<>();
public void addAttributes(AdditionalAttribute attribute) {
additionalAttributes.add(attribute);
attribute.setFindings(this);
}
public void removeAttributes(AdditionalAttribute attribute) {
additionalAttributes.remove(attribute);
attribute.setFindings(null);
}
}
And the child class:
@Entity(name = "additional_attribute")
public class AdditionalAttribute {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private String attributeName;
@Column(length = 255)
private String attributeValue;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "master_id")
private Master master;
}
Now let's say I already have inserted data in both the tables like:
Master
================
id. name
1. xyz
AdditionalAttribute
======================================================
id. attributeName. attributeValue. master_id
1 Acme test 1
2. loreal red 1
Now given the master_id, how do I update the attributeValue in the AdditionalAttribute table for that id??
What I have done is:
Get the list of
additionalAttributesfrom the master entity (the one we got from session)Iterate over the list and get each
AdditionAttributeobject.Set the new value to the object.
Is it a good way of updating? Do we have any better approach?