[英]Springboot Mapping and DTO
我是 Spring 的新手,在插入下面的示例時遇到了很多疑問。 我目前有三個表,其模型如下:
@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;
}
這是我的 DTO:
public class HistoriqueDeploiementReadingDTO {
@NotNull
private Integer id;
@NotNull
private String namespacename;
@NotNull
private String servicename;
@NotNull
private String tagvalue;
}
所以問題是:我收到一個 HistoriqueDeploiementReadingDTO 類型的對象,我必須將它插入到 historiquedeploiement 表中。 問題是我收到了“namespacename”、“servicename”,我需要保存我可以在表命名空間和服務中找到的每個 ID。
當我有每個人的 id 時,我可以將它保存在 historiquedeploiement 表中。
我希望你理解這個小問題,並希望你能給我一些東西:) 謝謝!
你有兩種方式:
首先
如果關系是多對一,您的字段是服務列表和名稱空間列表,而不是服務和名稱空間。
如果你的意思是一對一
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 -
您應該首先驗證您收到的內容(針對每個表的數據庫記錄)。 以下或多或少會給你一個亮點,所以你也應該為其他人做。 不要忘記,所有人都應該在同一個事務上。
==更新==
@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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.