Reputation: 37
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?
Upvotes: 1
Views: 1040
Reputation: 737
The objects of PerformanceIndicator class require Activity/ArchitectualLayer objects. If you only have the identifiers of those objects, you can obtain references to those objects by calling the getOne
method from a corresponding repository.
The getOne
method does not load an object from a database, but creates a proxy/reference object (under the hood it calls the getReference
method of EntityManager
). It's recommended exactly in such scenarios where you have an id and only want to reference an object.
So, your code could look like:
PerformanceIndicator p = new PeformanceIndicator();
p.setName("test");
p.setDescription("test");
p.setLevel(1);
p.setActivity(activityRepository.getOne(1));
p.setArchitecturalLayer(architecturalLayerRepository.getOne(1));
performanceIndicatorRepository.save(p);
Usually, such code is inside a service method (like save/create
) which is responsible for 'filling gaps' and performing all extra work to properly save an object.
Upvotes: 4