[英]null value in column "id" of relation "technologies" violates not-null constraint (PostgreSQL)
Technology and technologyManager classes as shown below: Im trying to add a technology to database by swagger-ui but get an exception of null value in column "id" of relation "technologies" violates not-null constraint 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);
}
}
I tried to add a new Technology to database by using swagger-ui but it threw an exception of null value in column "id" even though it is primary key and auto-generated.我尝试使用 swagger-ui 向数据库添加一项新技术,但它在“id”列中抛出了 null 值的异常,即使它是主键并且是自动生成的。
Try with GenerationType.AUTO
instead of GenerationType.IDENTITY
and change the type of the id from int
to Integer
or Long
尝试使用
GenerationType.AUTO
而不是GenerationType.IDENTITY
并将 id 的类型从int
更改为Integer
或Long
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.