My application contains one class that looks like this:
import javax.persistence.*;
@Entity
public class PerformanceIndicator {
@Id @GeneratedValue private Long id;
private String name;
private String description;
private int level;
/**
* @JoinColumn says that Performance Indicator table will contain a separate column
* ARCHITECTURAL_LAYER_ID which will eventually act as a foreign key reference to primary key of
* Accountability Model table. @ManyToOne says that multiple Architectural Layers can refer to
* same Accountability Model (Multiple Architectural Layers can be registered in same
* Accountability Model). Additionally , with optional=false we make sure that Architectural Layer
* can exist without a Accountability Model.
*/
@ManyToOne()
@JoinColumn(name = "activity_id")
private Activity activity;
@ManyToOne()
@JoinColumn(name = "architectural_layer_id")
private ArchitecturalLayer architecturalLayer;
public PerformanceIndicator() {}
public PerformanceIndicator(String name, String description, int level) {
this.name = name;
this.description = description;
this.level = level;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public Activity getActivity() {
return activity;
}
public void setActivity(Activity activity) {
this.activity = activity;
}
public ArchitecturalLayer getArchitecturalLayer() {
return architecturalLayer;
}
public void setArchitecturalLayer(ArchitecturalLayer architecturalLayer) {
this.architecturalLayer = architecturalLayer;
}
@Override
public String toString() {
return "PerformanceIndicator{"
+ "id="
+ id
+ ", name='"
+ name
+ '\''
+ ", description='"
+ description
+ '\''
+ ", level="
+ level
+ ", activity="
+ activity
+ ", architecturalLayer="
+ architecturalLayer
+ '}';
}
}
And architectural layer class:
import com.fasterxml.jackson.annotation.JsonBackReference;
import javax.persistence.*;
@Entity
public class ArchitecturalLayer {
@Id @GeneratedValue private Long id;
private String name;
private String description;
/**
* @JoinColumn says that Architectural Layer table will contain a separate column
* ARCHITECTURAL_LAYER_ID which will eventually act as a foreign key reference to primary key of
* Accountability Model table. @ManyToOne says that multiple Architectural Layers can refer to
* same Accountability Model (Multiple Architectural Layers can be registered in same
* Accountability Model). Additionally , with optional=false we make sure that Architectural Layer
* can exist without a Accountability Model.
*/
@ManyToOne
@JoinColumn(name = "accountablility_model_id")
@JsonBackReference
private AccountablityModel accountablityModel;
public ArchitecturalLayer() {}
public ArchitecturalLayer(Long id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public ArchitecturalLayer(String name, String description) {
this.name = name;
this.description = description;
}
public ArchitecturalLayer(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public AccountablityModel getAccountablityModel() {
return accountablityModel;
}
public void setAccountablityModel(AccountablityModel accountablityModel) {
this.accountablityModel = accountablityModel;
}
@Override
public String toString() {
return "ArchitecturalLayer{"
+ "id="
+ id
+ ", name='"
+ name
+ '\''
+ ", description='"
+ description
+ '\''
+ ", accountablityModel="
+ accountablityModel
+ '}';
}
}
When I try to save a PerformanceIndicator object that already has a existing I want to do this by it's ID. But there also have to be an option to create an Activity and Architectural layer if the users wants to.
for example:
PerformanceIndicator p = new PeformanceIndicator();
p.setName("test");
p.setDescription("test");
p.setLevel(1);
p.setActivity(1);
p.setArchitecturalLayer(1);
performanceIndicatorService.save(p);
What can I do to be able to persist this by passing an ID but also be able to pass in a object of the Activity/ArchitecturalLayer?