【问题标题】:Springboot Mapping and DTOSpringboot 映射和 DTO
【发布时间】:2020-10-27 04:41:44
【问题描述】:

我是 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 后,我可以将其保存在 historiquedeployement 表中。

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

【问题讨论】:

    标签: java spring-boot jpa spring-data-jpa


    【解决方案1】:

    您应该首先验证您收到的内容(针对每个表的数据库记录)。以下或多或少会给你一个亮点,所以你也应该为其他人做。 不要忘记所有人都应该在同一个事务中。

    == 更新==

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

    【讨论】:

    • 有件事让我很困扰,我们从来不讨论服务/命名空间表的id,你使用deploiment.setService(service),你能解释一下为什么吗?跨度>
    • 休眠它自己会为你处理。
    • 好的,谢谢!太漂亮了:) 我会验证你的答案,只是为了确定,我在 historiquedeployement 上放了 ManyToOne 注释。我应该把 OneToMany 放在服务/命名空间上吗?
    • 这取决于您的需求。它被称为双向多对一。如果您想从ServicesNamespace 等获取HistoriqueDeploiementYES。如果您从HistoriqueDeploiement 获取所有其他人,NO。在这里查看全面的解释thorben-janssen.com/…
    【解决方案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 -

    【讨论】:

    • 一个(服务或命名空间)可以在许多“historiquedeploiement”中指定在 historiqueDeploiement.setService(service) 我看不到任何“id”它在哪里?我必须将 idservice 和 idnamespacename 保存在 historiqueDeploiement 谢谢
    • 你可以添加类,因为你的变量不是整数,是一个服务/命名空间类
    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多