英文:
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”,我需要保存每个名称在namespace和service表中对应的id。
当我拥有每个名称的id后,我可以将其保存在historiquedeploiement表中。
我希望你能理解这个小问题,并希望你能提出一些建议
谢谢!
英文:
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 !
答案1
得分: 1
你有两种方式:
首先,
如果关系是多对一,那么你的字段是服务列表和命名空间列表,而不是服务和命名空间。
如果你指的是一对一关系,
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 -
英文:
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 -
答案2
得分: 1
你应该首先验证接收到的内容(与每个表的数据库记录进行对比)。以下大致内容将为您提供重点,您也应该对其他部分进行类似操作。
不要忘记所有操作都应在同一事务中。
== 更新内容 ==
@Transactional(rollbackFor=Exception.class)
public boolean saveHistoriqueDeploiement(HistoriqueDeploiementReadingDTO dto) {
Services service = getServices(dto.getServicename());
// 对其他部分也执行相同操作
HistoriqueDeploiement deploiment = new HistoriqueDeploiement();
deploiment.setService(service);
// 对其他部分也执行相同操作
deploiementRepository.save(deploiment);
}
public Services getServices(String serviceName) {
Services service = serviceRepository.findByServicename(serviceName); // 如果存在,使用现有的
if (service == null) {
service = new Services();
service.setServicename(dto.getServicename());
service = serviceRepository.save(service);
}
return service;
}
(请注意,代码中的注释部分未翻译。)
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论