![](/img/trans.png)
[英]JPA null value in column "header_id" of relation "invoice" violates not-null constraint with PostgresSQL
[英]null value in column "id" of relation "technologies" violates not-null constraint (PostgreSQL)
Technology 和 technologyManager 類如下所示:我試圖通過 swagger-ui 將技術添加到數據庫,但在關系“technologies”的“id”列中出現 null 值的異常違反了非空約束
public class Technology {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@ManyToOne(cascade = CascadeType.DETACH)
@JoinColumn(name = "language_id")
private ProgrammingLanguage language;
}
public void add(CreateTechnologyRequest technologyRequest) throws Exception {
Technology technology = new Technology();
if(technologyRequest.getName().isBlank()) {
throw new Exception("Please enter a name");
}else{
for(Technology technologies : technologyRepository.findAll()) {
if(technologies.getName().equalsIgnoreCase(technologyRequest.getName())) {
throw new Exception("This name is already exist.");
}else {
} technology.setName(technologyRequest.getName());
for(ProgrammingLanguage language : languageRepository.findAll()) {
if(language.getName().equalsIgnoreCase(technologyRequest.getLanguageName())) {
technology.setLanguage(language);
}
}
}
technologyRepository.save(technology);
}
}
我嘗試使用 swagger-ui 向數據庫添加一項新技術,但它在“id”列中拋出了 null 值的異常,即使它是主鍵並且是自動生成的。
嘗試使用GenerationType.AUTO
而不是GenerationType.IDENTITY
並將 id 的類型從int
更改為Integer
或Long
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.