Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Springboot映射与DTO_Java_Spring Boot_Jpa_Spring Data Jpa - Fatal编程技术网

Java Springboot映射与DTO

Java Springboot映射与DTO,java,spring-boot,jpa,spring-data-jpa,Java,Spring Boot,Jpa,Spring Data Jpa,我刚接触Spring,在插入下面的示例时遇到了很多疑问。 我目前有三个表格,其模型如下: @Entity @Table(name = "namespace") public class Namespace { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @NotNull @Size(max = 100) @Co

我刚接触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;
}
这是我的DTO:

public class HistoriqueDeploiementReadingDTO {
        @NotNull
        private Integer id;

        @NotNull
        private String namespacename;

        @NotNull
        private String servicename;
        
        @NotNull
        private String tagvalue;

}
所以问题是: 我收到一个类型为HistoryQuedeLoiementTreading DTO的对象,我必须将其插入HistoryQuedeLoiement表中。 问题是我收到了“namespacename”、“servicename”,我需要保存在表namespace和service中可以找到的每个名称的id

当我拥有每一个的id时,我可以将其保存在HistoryQueeDevelopment表中

我希望你能理解这个小问题,并希望你能给我一些建议:) 谢谢

您有两种方法:

首先

如果关系为多对一,则字段是服务列表和名称空间列表,而不是服务和名称空间列表

如果你指的是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-

您应该首先验证您收到的内容(对照每个表的db记录)。以下或多或少会给你一个亮点,所以你也应该为其他人做。 别忘了所有交易都应该在同一笔交易中。

==已更新==


一个(服务或名称空间)可以在HistoryQueeDevelopment中的许多“HistoryQueeDevelopment”中指定。setService(服务)我看不到任何“id”在哪里?我必须在HistoryQueeDeployment中保存idservice和idnamespacename谢谢您可以添加类,因为您的变量不是整数,它是一个服务/命名空间类有一些问题困扰着我,在任何时候我们都不谈论服务/命名空间表的id,您使用deployment.setService(service),你能解释一下为什么吗?休眠它自己会帮你处理的。好的,谢谢!这很美:)我会验证你的答案,只是为了确定,我在历史开发上做了很多注释。我应该把OneToMany放在服务/命名空间上吗?这取决于您的需求。它被称为双向多对一。如果您想从
服务
命名空间
等获取
历史开发
,请选择“是”。如果您从
HistoryQueeDevelopment
获取所有其他信息,则为否。请检查此处以获取全面的解释
public class HistoriqueDeploiementReadingDTO {
        @NotNull
        private Integer id;

        @NotNull
        private String namespacename;

        @NotNull
        private String servicename;
        
        @NotNull
        private String tagvalue;

}
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);
@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;
}