【发布时间】: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