java更改布尔值Spring数据JPA
我正在编写一个简单的代码,其中我需要更改一个布尔值,而不是删除数据库表中的整行。我用注释标记了适当的字段: “columnDefinition=”布尔默认值true“
但当我在数据库中保存一个新实例时,它并没有在上面标记true/false。然后我将默认值直接放在字段中:“private Boolean exist=true
它开始标记我的实例,但当我尝试更改值时,它在两种情况下都不起作用。(在第一种情况下,我不能将null更改为false,在第二种情况下,将true更改为false)
我使用PostgreSQL,我的解决方案来自官方文档Default Column Values in JPA
我的实体类:
private long id;
private String colour;
private Boolean exist = true;
public Colour() {
}
@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
return id;
}
@Column(name = "colour", unique = true)
public String getColour() {
return colour;
}
@Column(name = "exist", columnDefinition = "boolean default true")
public Boolean getExist() {
return exist;
}
public void setId(long id) {
this.id = id;
}
public void setColour(String colour) {
this.colour = colour;
}
public void setExist(Boolean exist) {
exist = exist;
}
我的删除方法:
@Override
public void deleteColour(long id) {
Colour colour = getOne(id);
try {
colour.setExist(false);
colourRepository.save(colour);
} catch (NullPointerException e) {
System.out.println("delete is not possible: colour doesn't exist");
}
}
运行方法deleteColour()后的表“colors”:in case of annotation
运行方法deleteColour()后的表“colors”:in case of field default
谢谢你抽出时间
共 (0) 个答案