简体   繁体   English

Springboot 映射和 DTO

[英]Springboot Mapping and DTO

I'm new to Spring and I'm encountering a lot of doubts when making an insertion of the example below.我是 Spring 的新手,在插入下面的示例时遇到了很多疑问。 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 :这是我的 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.所以问题是:我收到一个 HistoriqueDeploiementReadingDTO 类型的对象,我必须将它插入到 historiquedeploiement 表中。 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.问题是我收到了“namespacename”、“servicename”,我需要保存我可以在表命名空间和服务中找到的每个 ID。

When i have the id of each one, i can save it in historiquedeploiement table.当我有每个人的 id 时,我可以将它保存在 historiquedeploiement 表中。

I hope you understand that little problem and hope you can purpose me something :) Thanks !我希望你理解这个小问题,并希望你能给我一些东西:) 谢谢!

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 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM