Springboot映射和DTO

时间:2020-10-26 20:02:54

标签: 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)
    @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表中。 问题是我收到“名称空间名称”,“服务名称”,并且我需要保存在表名称空间和服务中可以找到的每个ID。

当我拥有每个ID时,可以将其保存在historiquedeploiement表中。

我希望你能理解这个小问题,并希望你可以给我一些东西:) 谢谢!

2 个答案:

答案 0 :(得分:1)

您有2种方法:

首先

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

如果您的意思是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-

答案 1 :(得分:1)

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

==更新==

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