Reputation: 557
I'm new to Spring and I'm encountering a lot of doubts when making an insertion of the example below. I currently have three tables whose models are those below:
@Entity
@Table(name = "namespace")
public class Namespace {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
@Size(max = 100)
@Column(unique = true)
private String namespacename;
}
@Entity
@Table(name = "services")
public class Services {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
@Size(max = 100)
@Column(unique = true)
private String servicename;
}
@Entity
@Table(name = "historiquedeploiement")
public class HistoriqueDeploiement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "idnamespace", nullable = false)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
@JsonProperty("idnamespace")
private Namespace namespace;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "idservices", nullable = false)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
@JsonProperty("idservices")
private Services services;
@NotNull
@Size(max = 100)
@Column(unique = true)
private String tagvalue;
}
And this is my DTO :
public class HistoriqueDeploiementReadingDTO {
@NotNull
private Integer id;
@NotNull
private String namespacename;
@NotNull
private String servicename;
@NotNull
private String tagvalue;
}
So the problem is : I receive an object of type HistoriqueDeploiementReadingDTO and i have to insert it in historiquedeploiement table. The problem is that i receive "namespacename", "servicename" and i need to save the id of each one that i can find in the table namespace and service.
When i have the id of each one, i can save it in historiquedeploiement table.
I hope you understand that little problem and hope you can purpose me something :) Thanks !
Upvotes: 1
Views: 582
Reputation: 1754
You should first validate what you receive(against db records of each table). More or less the following will give you a highlight, so you should do for the others too. Don't forget that all should be on the same transaction.
== updated==
@Transactional(rollbackFor=Exception.class)
public boolean saveHistoriqueDeploiement(HistoriqueDeploiementReadingDTO dto) {
Services service = getServices(dto.getServicename());
// do the same for the others
HistoriqueDeploiement deploiment = new HistoriqueDeploiement();
deploiment.setService(service);
//same for the others
deploiementRepository.save(deploiment);
}
public Services getServices(String serviceName) {
Services service = serviceRepository.findByServicename(serviceName); //if it exists, use the existing
if(service == null) {
service = new Services();
service.setServicename(dto.getServicename());
service = serviceRepository.save(service);
}
return service;
}
Upvotes: 1
Reputation: 11
You have 2 ways:
First of all
if relation is many to one your field is List of services and List of namespaces instead services and namespaces.
if you mean OneToOne
HistoriqueDeploiementReadingDTO historiqueDeploiementReadingDTO;
NameSpace nameSpace = new Namespace();
namespace.setNameSpaceName(historiqueDeploiementReadingDTO.getNameSpaceName());
Services service = new Service();
service.setServicename(historiqueDeploiementReadingDTO.getServiceName())
HistoriqueDeploiement historiqueDeploiement = new HistoriqueDeploiement;
historiqueDeploiement.setTagValue(historiqueDeploiementReadingDTO.getTagValue())
historiqueDeploiement.setService(service)
historiqueDeploiement.setNameSpaceName(nameSpace)
IHistoriqueDeploiementRepository.save(historiqueDeploiement);
2 -
Upvotes: 1