春季启动映射和数据传输对象 (DTO)

huangapple go评论78阅读模式
英文:

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表中。

我希望你能理解这个小问题,并希望你能提出一些建议 春季启动映射和数据传输对象 (DTO)
谢谢!

英文:

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 春季启动映射和数据传输对象 (DTO)
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;
}

huangapple
  • 本文由 发表于 2020年10月27日 04:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64544203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定